Skip to content

Commit c717b74

Browse files
committed
Fix invalid classpath for tests in maven plugin
1 parent 6afe88a commit c717b74

File tree

13 files changed

+63
-62
lines changed

13 files changed

+63
-62
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![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)
1+
[![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)
22
# Why async-await?
33
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.
44

@@ -17,7 +17,7 @@ First, add Maven dependency to the library runtime:
1717
<dependency>
1818
<groupId>net.tascalate.async</groupId>
1919
<artifactId>net.tascalate.async.runtime</artifactId>
20-
<version>1.2.0</version>
20+
<version>1.2.1</version>
2121
</dependency>
2222
```
2323
Second, add the following build plugins in the specified order:
@@ -27,7 +27,7 @@ Second, add the following build plugins in the specified order:
2727
<plugin>
2828
<groupId>net.tascalate.async</groupId>
2929
<artifactId>net.tascalate.async.tools.maven</artifactId>
30-
<version>1.2.0</version>
30+
<version>1.2.1</version>
3131
<executions>
3232
<execution>
3333
<phase>process-classes</phase>
@@ -40,7 +40,7 @@ Second, add the following build plugins in the specified order:
4040
<plugin>
4141
<groupId>net.tascalate.javaflow</groupId>
4242
<artifactId>net.tascalate.javaflow.tools.maven</artifactId>
43-
<version>2.7.0</version>
43+
<version>2.7.1</version>
4444
<executions>
4545
<execution>
4646
<phase>process-classes</phase>

net.tascalate.async.agent/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>net.tascalate.async</groupId>
77
<artifactId>net.tascalate.async.parent</artifactId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../</relativePath>
1010
</parent>
1111

net.tascalate.async.examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>net.tascalate.async</groupId>
77
<artifactId>net.tascalate.async.parent</artifactId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../</relativePath>
1010
</parent>
1111

net.tascalate.async.extras/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>net.tascalate.async</groupId>
77
<artifactId>net.tascalate.async.parent</artifactId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../</relativePath>
1010
</parent>
1111

net.tascalate.async.resolver.propagated/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>net.tascalate.async</groupId>
77
<artifactId>net.tascalate.async.parent</artifactId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../</relativePath>
1010
</parent>
1111

net.tascalate.async.resolver.provided/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>net.tascalate.async</groupId>
77
<artifactId>net.tascalate.async.parent</artifactId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../</relativePath>
1010
</parent>
1111

net.tascalate.async.resolver.scoped/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>net.tascalate.async</groupId>
77
<artifactId>net.tascalate.async.parent</artifactId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../</relativePath>
1010
</parent>
1111

net.tascalate.async.resolver.swing/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>net.tascalate.async</groupId>
77
<artifactId>net.tascalate.async.parent</artifactId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../</relativePath>
1010
</parent>
1111

net.tascalate.async.runtime/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>net.tascalate.async</groupId>
77
<artifactId>net.tascalate.async.parent</artifactId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../</relativePath>
1010
</parent>
1111

net.tascalate.async.tools.core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>net.tascalate.async</groupId>
77
<artifactId>net.tascalate.async.parent</artifactId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../</relativePath>
1010
</parent>
1111

net.tascalate.async.tools.maven/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>net.tascalate.async</groupId>
77
<artifactId>net.tascalate.async.parent</artifactId>
8-
<version>1.2.0</version>
8+
<version>1.2.1</version>
99
<relativePath>../</relativePath>
1010
</parent>
1111

net.tascalate.async.tools.maven/src/main/java/net/tascalate/async/tools/maven/AsyncAwaitEnhancerMojo.java

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@
4444

4545
import org.apache.commons.javaflow.spi.ClasspathResourceLoader;
4646
import org.apache.maven.plugin.AbstractMojo;
47+
import org.apache.maven.plugin.MojoExecution;
4748
import org.apache.maven.plugin.MojoExecutionException;
4849
import org.apache.maven.plugin.logging.Log;
50+
import org.apache.maven.plugins.annotations.Component;
4951
import org.apache.maven.plugins.annotations.LifecyclePhase;
5052
import org.apache.maven.plugins.annotations.Mojo;
5153
import org.apache.maven.plugins.annotations.Parameter;
@@ -71,7 +73,7 @@
7173
* </pre>
7274
*
7375
*/
74-
@Mojo(name = "tascalate-async-enhance", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
76+
@Mojo(name = "tascalate-async-enhance", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.TEST /* ALL DEPENDENCIES */)
7577
public class AsyncAwaitEnhancerMojo extends AbstractMojo {
7678

7779
@Parameter(defaultValue = "${project}", property = "tascalate-async.enhancer.project", required = true, readonly = true)
@@ -102,6 +104,9 @@ public class AsyncAwaitEnhancerMojo extends AbstractMojo {
102104
*/
103105
@Parameter(property = "tascalate-async.enhancer.testBuildDir", required = false)
104106
private String testBuildDir;
107+
108+
@Component
109+
private MojoExecution execution;
105110

106111
public void execute() throws MojoExecutionException {
107112
Log log = getLog();
@@ -110,65 +115,59 @@ public void execute() throws MojoExecutionException {
110115
return;
111116
}
112117

113-
ClassLoader originalContextClassLoader = currentThread().getContextClassLoader();
114118
try {
115-
List<URL> classPath = new ArrayList<URL>();
116-
117-
for (String runtimeResource : project.getRuntimeClasspathElements()) {
118-
classPath.add(resolveUrl(new File(runtimeResource)));
119-
}
120-
121-
File inputDirectory = buildDir == null ?
119+
File mainInputDirectory = buildDir == null ?
122120
new File(project.getBuild().getOutputDirectory()) : computeDir(buildDir);
123121

124-
classPath.add(resolveUrl(inputDirectory));
125-
126-
ClassLoader effectiveClassLoader = loadAdditionalClassPath(classPath);
127-
AsyncAwaitClassFileGenerator generator = new AsyncAwaitClassFileGenerator(
128-
new ClasspathResourceLoader(effectiveClassLoader)
129-
);
130-
131-
// final ResourceTransformer dirTransformer =
132-
// RewritingUtils.createTransformer(
133-
// classPath.toArray(new URL[]{}), TransformerType.ASM5
134-
// );
135-
136-
long now = System.currentTimeMillis();
137-
138-
for (File source : RecursiveFilesIterator.scanClassFiles(inputDirectory)) {
139-
if (source.lastModified() <= now) {
140-
log.debug("Applying async/await support: " + source);
141-
boolean rewritten = rewriteClassFile(source, generator, source);
142-
if (rewritten) {
143-
log.info("Rewritten async-enabled class file: " + source);
144-
}
145-
}
122+
if (mainInputDirectory.exists()) {
123+
// Use runtime instead of compile - runtime contains non less than compile
124+
transformFiles(mainInputDirectory, project.getRuntimeClasspathElements());
125+
} else {
126+
log.warn("No main build output directory available, skip enhancing main classes");
146127
}
147128

148129
if (includeTestClasses) {
149130
File testInputDirectory = testBuildDir == null ?
150131
new File(project.getBuild().getTestOutputDirectory()) : computeDir(testBuildDir);
151-
132+
152133
if (testInputDirectory.exists()) {
153-
for (File source : RecursiveFilesIterator.scanClassFiles(testInputDirectory)) {
154-
if (source.lastModified() <= now) {
155-
log.debug("Applying async/await support: " + source);
156-
final boolean rewritten = rewriteClassFile(source, generator, source);
157-
if (rewritten) {
158-
log.info("Rewritten async-enabled class file: " + source);
159-
}
160-
161-
}
162-
}
163-
}
134+
transformFiles(testInputDirectory, project.getTestClasspathElements());
135+
} else if ("process-test-classes".equals(execution.getLifecyclePhase())) {
136+
log.warn("No test build output directory available, skip enhancing test classes");
137+
}
164138
}
165139
} catch (Exception e) {
166140
getLog().error(e.getMessage(), e);
167141
throw new MojoExecutionException(e.getMessage(), e);
168-
} finally {
169-
currentThread().setContextClassLoader(originalContextClassLoader);
170142
}
171143
}
144+
145+
private void transformFiles(File inputDirectory, List<String> classPathEntries)
146+
throws IOException, IllegalClassFormatException {
147+
Log log = getLog();
148+
List<URL> classPath = new ArrayList<URL>();
149+
for (String classPathEntry : classPathEntries) {
150+
classPath.add(resolveUrl(new File(classPathEntry)));
151+
}
152+
classPath.add(resolveUrl(inputDirectory));
153+
ClassLoader effectiveClassLoader = loadAdditionalClassPath(classPath);
154+
AsyncAwaitClassFileGenerator generator = new AsyncAwaitClassFileGenerator(
155+
new ClasspathResourceLoader(effectiveClassLoader)
156+
);
157+
158+
long now = System.currentTimeMillis();
159+
160+
for (File source : RecursiveFilesIterator.scanClassFiles(inputDirectory)) {
161+
if (source.lastModified() <= now) {
162+
log.debug("Applying async/await support: " + source);
163+
boolean rewritten = rewriteClassFile(source, generator, source);
164+
if (rewritten) {
165+
log.info("Rewritten async-enabled class file: " + source);
166+
}
167+
}
168+
}
169+
170+
}
172171

173172
protected boolean rewriteClassFile(File source, AsyncAwaitClassFileGenerator generator, File target)
174173
throws IOException, IllegalClassFormatException {
@@ -199,6 +198,9 @@ protected boolean rewriteClassFile(File source, AsyncAwaitClassFileGenerator gen
199198

200199
private ClassLoader loadAdditionalClassPath(List<URL> classPath) {
201200
ClassLoader contextClassLoader = currentThread().getContextClassLoader();
201+
if (null == contextClassLoader) {
202+
contextClassLoader = ClassLoader.getSystemClassLoader();
203+
}
202204

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

211-
currentThread().setContextClassLoader(pluginClassLoader);
212213
return pluginClassLoader;
213214
}
214215

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>net.tascalate.async</groupId>
66
<artifactId>net.tascalate.async.parent</artifactId>
7-
<version>1.2.0</version>
7+
<version>1.2.1</version>
88
<packaging>pom</packaging>
99

1010
<name>Tascalate Async/Await</name>
@@ -51,7 +51,7 @@
5151
<properties>
5252
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5353
<tascalate.concurrent.version>0.9.6</tascalate.concurrent.version>
54-
<tascalate.javaflow.version>2.7.0</tascalate.javaflow.version>
54+
<tascalate.javaflow.version>2.7.1</tascalate.javaflow.version>
5555
<tascalate.javaflow.extras.version>2.4.2</tascalate.javaflow.extras.version>
5656
<tascalate.asmx.version>9.2.1</tascalate.asmx.version>
5757
<tascalate.instrument.version>1.3.0</tascalate.instrument.version>

0 commit comments

Comments
 (0)