Skip to content

Commit

Permalink
Fix invalid classpath for tests in maven plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vsilaev committed Feb 11, 2022
1 parent 6afe88a commit c717b74
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 62 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Maven Central](https://img.shields.io/maven-central/v/net.tascalate.async/net.tascalate.async.parent.svg)](https://search.maven.org/artifact/net.tascalate.async/net.tascalate.async.parent/1.2.0/pom) [![GitHub release](https://img.shields.io/github/release/vsilaev/tascalate-async-await.svg)](https://github.com/vsilaev/tascalate-async-await/releases/tag/1.2.0) [![license](https://img.shields.io/github/license/vsilaev/tascalate-async-await.svg)](https://github.com/vsilaev/tascalate-async-await/blob/master/LICENSE)
[![Maven Central](https://img.shields.io/maven-central/v/net.tascalate.async/net.tascalate.async.parent.svg)](https://search.maven.org/artifact/net.tascalate.async/net.tascalate.async.parent/1.2.1/pom) [![GitHub release](https://img.shields.io/github/release/vsilaev/tascalate-async-await.svg)](https://github.com/vsilaev/tascalate-async-await/releases/tag/1.2.1) [![license](https://img.shields.io/github/license/vsilaev/tascalate-async-await.svg)](https://github.com/vsilaev/tascalate-async-await/blob/master/LICENSE)
# Why async-await?
Asynchronous programming has long been a useful way to perform operations that don’t necessarily need to hold up the flow or responsiveness of an application. Generally, these are either compute-bound operations or I/O bound operations. Compute-bound operations are those where computations can be done on a separate thread, leaving the main thread to continue its own processing, while I/O bound operations involve work that takes place externally and may not need to block a thread while such work takes place. Common examples of I/O bound operations are file and network operations.

Expand All @@ -17,7 +17,7 @@ First, add Maven dependency to the library runtime:
<dependency>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.runtime</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</dependency>
```
Second, add the following build plugins in the specified order:
Expand All @@ -27,7 +27,7 @@ Second, add the following build plugins in the specified order:
<plugin>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.tools.maven</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<executions>
<execution>
<phase>process-classes</phase>
Expand All @@ -40,7 +40,7 @@ Second, add the following build plugins in the specified order:
<plugin>
<groupId>net.tascalate.javaflow</groupId>
<artifactId>net.tascalate.javaflow.tools.maven</artifactId>
<version>2.7.0</version>
<version>2.7.1</version>
<executions>
<execution>
<phase>process-classes</phase>
Expand Down
2 changes: 1 addition & 1 deletion net.tascalate.async.agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<relativePath>../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion net.tascalate.async.examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<relativePath>../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion net.tascalate.async.extras/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<relativePath>../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion net.tascalate.async.resolver.propagated/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<relativePath>../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion net.tascalate.async.resolver.provided/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<relativePath>../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion net.tascalate.async.resolver.scoped/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<relativePath>../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion net.tascalate.async.resolver.swing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<relativePath>../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion net.tascalate.async.runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<relativePath>../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion net.tascalate.async.tools.core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<relativePath>../</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion net.tascalate.async.tools.maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<relativePath>../</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@

import org.apache.commons.javaflow.spi.ClasspathResourceLoader;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -71,7 +73,7 @@
* </pre>
*
*/
@Mojo(name = "tascalate-async-enhance", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
@Mojo(name = "tascalate-async-enhance", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.TEST /* ALL DEPENDENCIES */)
public class AsyncAwaitEnhancerMojo extends AbstractMojo {

@Parameter(defaultValue = "${project}", property = "tascalate-async.enhancer.project", required = true, readonly = true)
Expand Down Expand Up @@ -102,6 +104,9 @@ public class AsyncAwaitEnhancerMojo extends AbstractMojo {
*/
@Parameter(property = "tascalate-async.enhancer.testBuildDir", required = false)
private String testBuildDir;

@Component
private MojoExecution execution;

public void execute() throws MojoExecutionException {
Log log = getLog();
Expand All @@ -110,65 +115,59 @@ public void execute() throws MojoExecutionException {
return;
}

ClassLoader originalContextClassLoader = currentThread().getContextClassLoader();
try {
List<URL> classPath = new ArrayList<URL>();

for (String runtimeResource : project.getRuntimeClasspathElements()) {
classPath.add(resolveUrl(new File(runtimeResource)));
}

File inputDirectory = buildDir == null ?
File mainInputDirectory = buildDir == null ?
new File(project.getBuild().getOutputDirectory()) : computeDir(buildDir);

classPath.add(resolveUrl(inputDirectory));

ClassLoader effectiveClassLoader = loadAdditionalClassPath(classPath);
AsyncAwaitClassFileGenerator generator = new AsyncAwaitClassFileGenerator(
new ClasspathResourceLoader(effectiveClassLoader)
);

// final ResourceTransformer dirTransformer =
// RewritingUtils.createTransformer(
// classPath.toArray(new URL[]{}), TransformerType.ASM5
// );

long now = System.currentTimeMillis();

for (File source : RecursiveFilesIterator.scanClassFiles(inputDirectory)) {
if (source.lastModified() <= now) {
log.debug("Applying async/await support: " + source);
boolean rewritten = rewriteClassFile(source, generator, source);
if (rewritten) {
log.info("Rewritten async-enabled class file: " + source);
}
}
if (mainInputDirectory.exists()) {
// Use runtime instead of compile - runtime contains non less than compile
transformFiles(mainInputDirectory, project.getRuntimeClasspathElements());
} else {
log.warn("No main build output directory available, skip enhancing main classes");
}

if (includeTestClasses) {
File testInputDirectory = testBuildDir == null ?
new File(project.getBuild().getTestOutputDirectory()) : computeDir(testBuildDir);

if (testInputDirectory.exists()) {
for (File source : RecursiveFilesIterator.scanClassFiles(testInputDirectory)) {
if (source.lastModified() <= now) {
log.debug("Applying async/await support: " + source);
final boolean rewritten = rewriteClassFile(source, generator, source);
if (rewritten) {
log.info("Rewritten async-enabled class file: " + source);
}

}
}
}
transformFiles(testInputDirectory, project.getTestClasspathElements());
} else if ("process-test-classes".equals(execution.getLifecyclePhase())) {
log.warn("No test build output directory available, skip enhancing test classes");
}
}
} catch (Exception e) {
getLog().error(e.getMessage(), e);
throw new MojoExecutionException(e.getMessage(), e);
} finally {
currentThread().setContextClassLoader(originalContextClassLoader);
}
}

private void transformFiles(File inputDirectory, List<String> classPathEntries)
throws IOException, IllegalClassFormatException {
Log log = getLog();
List<URL> classPath = new ArrayList<URL>();
for (String classPathEntry : classPathEntries) {
classPath.add(resolveUrl(new File(classPathEntry)));
}
classPath.add(resolveUrl(inputDirectory));
ClassLoader effectiveClassLoader = loadAdditionalClassPath(classPath);
AsyncAwaitClassFileGenerator generator = new AsyncAwaitClassFileGenerator(
new ClasspathResourceLoader(effectiveClassLoader)
);

long now = System.currentTimeMillis();

for (File source : RecursiveFilesIterator.scanClassFiles(inputDirectory)) {
if (source.lastModified() <= now) {
log.debug("Applying async/await support: " + source);
boolean rewritten = rewriteClassFile(source, generator, source);
if (rewritten) {
log.info("Rewritten async-enabled class file: " + source);
}
}
}

}

protected boolean rewriteClassFile(File source, AsyncAwaitClassFileGenerator generator, File target)
throws IOException, IllegalClassFormatException {
Expand Down Expand Up @@ -199,6 +198,9 @@ protected boolean rewriteClassFile(File source, AsyncAwaitClassFileGenerator gen

private ClassLoader loadAdditionalClassPath(List<URL> classPath) {
ClassLoader contextClassLoader = currentThread().getContextClassLoader();
if (null == contextClassLoader) {
contextClassLoader = ClassLoader.getSystemClassLoader();
}

if (classPath.isEmpty()) {
return contextClassLoader;
Expand All @@ -208,7 +210,6 @@ private ClassLoader loadAdditionalClassPath(List<URL> classPath) {
classPath.toArray(new URL[classPath.size()]), contextClassLoader
);

currentThread().setContextClassLoader(pluginClassLoader);
return pluginClassLoader;
}

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>net.tascalate.async</groupId>
<artifactId>net.tascalate.async.parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<packaging>pom</packaging>

<name>Tascalate Async/Await</name>
Expand Down Expand Up @@ -51,7 +51,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<tascalate.concurrent.version>0.9.6</tascalate.concurrent.version>
<tascalate.javaflow.version>2.7.0</tascalate.javaflow.version>
<tascalate.javaflow.version>2.7.1</tascalate.javaflow.version>
<tascalate.javaflow.extras.version>2.4.2</tascalate.javaflow.extras.version>
<tascalate.asmx.version>9.2.1</tascalate.asmx.version>
<tascalate.instrument.version>1.3.0</tascalate.instrument.version>
Expand Down

0 comments on commit c717b74

Please sign in to comment.