Skip to content

Commit 47eac15

Browse files
#322, enable to control the exec:java interaction with JVM classloader more finely (#337)
Co-authored-by: Slawomir Jaranowski <[email protected]>
1 parent 582aed0 commit 47eac15

File tree

6 files changed

+170
-83
lines changed

6 files changed

+170
-83
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invoker.goals = clean compile exec:java

src/it/projects/github322/pom.xml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>org.codehaus.mojo.exec.it</groupId>
6+
<artifactId>parent</artifactId>
7+
<version>0.1</version>
8+
</parent>
9+
10+
<groupId>org.cb.maven.plugins.exec</groupId>
11+
<artifactId>github322</artifactId>
12+
<version>0.1</version>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>xml-apis</groupId>
17+
<artifactId>xml-apis</artifactId>
18+
<version>1.4.01</version>
19+
</dependency>
20+
</dependencies>
21+
22+
<build>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.codehaus.mojo</groupId>
26+
<artifactId>exec-maven-plugin</artifactId>
27+
<version>@pom.version@</version>
28+
<executions>
29+
<execution>
30+
<phase>test</phase>
31+
<goals>
32+
<goal>java</goal>
33+
</goals>
34+
</execution>
35+
</executions>
36+
<configuration>
37+
<mainClass>org.codehaus.mojo.exec.it.github322.Main</mainClass>
38+
</configuration>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.codehaus.mojo.exec.it.github322;
2+
3+
import javax.xml.transform.sax.SAXTransformerFactory;
4+
5+
public class Main {
6+
public static void main(final String... args) {
7+
System.out.println(
8+
"Main Result: <" +
9+
(
10+
SAXTransformerFactory.class.getProtectionDomain().getCodeSource() != null ?
11+
SAXTransformerFactory.class.getProtectionDomain().getCodeSource().getLocation() :
12+
null
13+
) +
14+
">");
15+
}
16+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
File log = new File(basedir, 'build.log')
21+
assert log.text.contains( 'Main Result: <null>')

src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,23 @@ public class ExecJavaMojo extends AbstractExecMojo {
194194
@Parameter
195195
private List<String> classpathFilenameExclusions;
196196

197+
/**
198+
* Additional packages to load from the jvm even if a classpath dependency matches.
199+
*
200+
* @since 3.5.0
201+
*/
202+
@Parameter
203+
private List<String> forcedJvmPackages;
204+
205+
/**
206+
* Additional packages to NOT load from the jvm even if it is in a flat classpath.
207+
* Can enable to reproduce a webapp behavior for example where library is loaded over the JVM.
208+
*
209+
* @since 3.5.0
210+
*/
211+
@Parameter
212+
private List<String> excludedJvmPackages;
213+
197214
/**
198215
* Whether to try and prohibit the called Java program from terminating the JVM (and with it the whole Maven build)
199216
* by calling {@link System#exit(int)}. When active, loaded classes will replace this call by a custom callback.
@@ -678,12 +695,13 @@ private URLClassLoader getClassLoader() throws MojoExecutionException {
678695
this.addRelevantPluginDependenciesToClasspath(classpathURLs);
679696
this.addRelevantProjectDependenciesToClasspath(classpathURLs);
680697
this.addAdditionalClasspathElements(classpathURLs);
681-
682698
try {
683699
final URLClassLoaderBuilder builder = URLClassLoaderBuilder.builder()
684700
.setLogger(getLog())
685701
.setPaths(classpathURLs)
686-
.setExclusions(classpathFilenameExclusions);
702+
.setExclusions(classpathFilenameExclusions)
703+
.setForcedJvmPackages(forcedJvmPackages)
704+
.setExcludedJvmPackages(excludedJvmPackages);
687705
if (blockSystemExit) {
688706
builder.setTransformer(new BlockExitTransformer());
689707
}

src/main/java/org/codehaus/mojo/exec/URLClassLoaderBuilder.java

+69-81
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@
4242
import static java.util.Arrays.asList;
4343

4444
/**
45-
*
4645
* @author Robert Scholte
4746
* @since 3.0.0
4847
*/
4948
class URLClassLoaderBuilder {
5049
private Log logger;
50+
private List<String> forcedJvmPackages;
51+
private List<String> excludedJvmPackages;
5152
private Collection<Path> paths;
5253
private Collection<String> exclusions;
5354
private ClassFileTransformer transformer;
@@ -58,6 +59,16 @@ static URLClassLoaderBuilder builder() {
5859
return new URLClassLoaderBuilder();
5960
}
6061

62+
URLClassLoaderBuilder setExcludedJvmPackages(List<String> excludedJvmPackages) {
63+
this.excludedJvmPackages = excludedJvmPackages;
64+
return this;
65+
}
66+
67+
URLClassLoaderBuilder setForcedJvmPackages(List<String> forcedJvmPackages) {
68+
this.forcedJvmPackages = forcedJvmPackages;
69+
return this;
70+
}
71+
6172
public URLClassLoaderBuilder setTransformer(final ClassFileTransformer transformer) {
6273
this.transformer = transformer;
6374
return this;
@@ -96,7 +107,7 @@ URLClassLoader build() throws IOException {
96107
}
97108
}
98109

99-
return new ExecJavaClassLoader(urls.toArray(new URL[0]), transformer, logger);
110+
return new ExecJavaClassLoader(urls.toArray(new URL[0]), transformer, forcedJvmPackages, excludedJvmPackages);
100111
}
101112

102113
// child first strategy
@@ -110,14 +121,20 @@ private static class ExecJavaClassLoader extends URLClassLoader {
110121
}
111122

112123
private final String jre;
113-
private final Log logger;
114124
private final ClassFileTransformer transformer;
115-
116-
public ExecJavaClassLoader(final URL[] urls, final ClassFileTransformer transformer, final Log logger) {
125+
private final List<String> forcedJvmPackages;
126+
private final List<String> excludedJvmPackages;
127+
128+
public ExecJavaClassLoader(
129+
URL[] urls,
130+
ClassFileTransformer transformer,
131+
List<String> forcedJvmPackages,
132+
List<String> excludedJvmPackages) {
117133
super(urls);
118134
this.jre = getJre();
119-
this.logger = logger;
120135
this.transformer = transformer;
136+
this.forcedJvmPackages = forcedJvmPackages;
137+
this.excludedJvmPackages = excludedJvmPackages;
121138
}
122139

123140
@Override
@@ -307,130 +324,101 @@ private boolean postLoad(boolean resolve, Class<?> clazz) {
307324
return false;
308325
}
309326

310-
// not all jvm classes, for ex "javax" can be overriden so don't list it here
327+
// not all jvm classes, for ex "javax" can be overridden so don't list it them all here (javax.resource for ex)
311328
private boolean isDirectJvmClass(final String name) {
329+
if (excludedJvmPackages != null && excludedJvmPackages.stream().anyMatch(name::startsWith)) {
330+
return false;
331+
}
312332
if (name.startsWith("java.")) {
313333
return true;
314-
}
315-
if (name.startsWith("sun.")) {
334+
} else if (name.startsWith("javax.")) {
335+
final String sub = name.substring("javax.".length());
336+
if (sub.startsWith("xml.")) {
337+
return true;
338+
}
339+
} else if (name.startsWith("sun.")) {
316340
return true;
317-
}
318-
if (name.startsWith("jdk.")) {
341+
} else if (name.startsWith("jdk.")) {
319342
return true;
320-
}
321-
if (name.startsWith("oracle.")) {
343+
} else if (name.startsWith("oracle.")) {
322344
return true;
323-
}
324-
if (name.startsWith("javafx.")) {
345+
} else if (name.startsWith("javafx.")) {
325346
return true;
326-
}
327-
if (name.startsWith("netscape.")) {
347+
} else if (name.startsWith("netscape.")) {
328348
return true;
329-
}
330-
if (name.startsWith("org.")) {
349+
} else if (name.startsWith("org.")) {
331350
final String sub = name.substring("org.".length());
332351
if (sub.startsWith("w3c.dom.")) {
333352
return true;
334-
}
335-
if (sub.startsWith("omg.")) {
353+
} else if (sub.startsWith("omg.")) {
336354
return true;
337-
}
338-
if (sub.startsWith("xml.sax.")) {
355+
} else if (sub.startsWith("xml.sax.")) {
339356
return true;
340-
}
341-
if (sub.startsWith("ietf.jgss.")) {
357+
} else if (sub.startsWith("ietf.jgss.")) {
342358
return true;
343-
}
344-
if (sub.startsWith("jcp.xml.dsig.internal.")) {
359+
} else if (sub.startsWith("jcp.xml.dsig.internal.")) {
345360
return true;
346361
}
347-
}
348-
if (name.startsWith("com.")) {
362+
} else if (name.startsWith("com.")) {
349363
final String sub = name.substring("com.".length());
350364
if (sub.startsWith("oracle.")) {
351365
return true;
352-
}
353-
if (sub.startsWith("sun.")) {
366+
} else if (sub.startsWith("sun.")) {
354367
final String subSun = sub.substring("sun.".length());
355368
if (subSun.startsWith("accessibility.")) {
356369
return true;
357-
}
358-
if (subSun.startsWith("activation.")) {
370+
} else if (subSun.startsWith("activation.")) {
359371
return true;
360-
}
361-
if (subSun.startsWith("awt.")) {
372+
} else if (subSun.startsWith("awt.")) {
362373
return true;
363-
}
364-
if (subSun.startsWith("beans.")) {
374+
} else if (subSun.startsWith("beans.")) {
365375
return true;
366-
}
367-
if (subSun.startsWith("corba.se.")) {
376+
} else if (subSun.startsWith("corba.se.")) {
368377
return true;
369-
}
370-
if (subSun.startsWith("demo.jvmti.")) {
378+
} else if (subSun.startsWith("demo.jvmti.")) {
371379
return true;
372-
}
373-
if (subSun.startsWith("image.codec.jpeg.")) {
380+
} else if (subSun.startsWith("image.codec.jpeg.")) {
374381
return true;
375-
}
376-
if (subSun.startsWith("imageio.")) {
382+
} else if (subSun.startsWith("imageio.")) {
377383
return true;
378-
}
379-
if (subSun.startsWith("istack.internal.")) {
384+
} else if (subSun.startsWith("istack.internal.")) {
380385
return true;
381-
}
382-
if (subSun.startsWith("java.")) {
386+
} else if (subSun.startsWith("java.")) {
383387
return true;
384-
}
385-
if (subSun.startsWith("java_cup.")) {
388+
} else if (subSun.startsWith("java_cup.")) {
386389
return true;
387-
}
388-
if (subSun.startsWith("jmx.")) {
390+
} else if (subSun.startsWith("jmx.")) {
389391
return true;
390-
}
391-
if (subSun.startsWith("jndi.")) {
392+
} else if (subSun.startsWith("jndi.")) {
392393
return true;
393-
}
394-
if (subSun.startsWith("management.")) {
394+
} else if (subSun.startsWith("management.")) {
395395
return true;
396-
}
397-
if (subSun.startsWith("media.sound.")) {
396+
} else if (subSun.startsWith("media.sound.")) {
398397
return true;
399-
}
400-
if (subSun.startsWith("naming.internal.")) {
398+
} else if (subSun.startsWith("naming.internal.")) {
401399
return true;
402-
}
403-
if (subSun.startsWith("net.")) {
400+
} else if (subSun.startsWith("net.")) {
404401
return true;
405-
}
406-
if (subSun.startsWith("nio.")) {
402+
} else if (subSun.startsWith("nio.")) {
407403
return true;
408-
}
409-
if (subSun.startsWith("org.")) {
404+
} else if (subSun.startsWith("org.")) {
410405
return true;
411-
}
412-
if (subSun.startsWith("rmi.rmid.")) {
406+
} else if (subSun.startsWith("rmi.rmid.")) {
413407
return true;
414-
}
415-
if (subSun.startsWith("rowset.")) {
408+
} else if (subSun.startsWith("rowset.")) {
416409
return true;
417-
}
418-
if (subSun.startsWith("security.")) {
410+
} else if (subSun.startsWith("security.")) {
419411
return true;
420-
}
421-
if (subSun.startsWith("swing.")) {
412+
} else if (subSun.startsWith("swing.")) {
422413
return true;
423-
}
424-
if (subSun.startsWith("tracing.")) {
414+
} else if (subSun.startsWith("tracing.")) {
425415
return true;
426-
}
427-
if (subSun.startsWith("xml.internal.")) {
416+
} else if (subSun.startsWith("xml.internal.")) {
428417
return true;
429418
}
430-
return false;
431419
}
432420
}
433-
return false;
421+
return forcedJvmPackages != null && forcedJvmPackages.stream().anyMatch(name::startsWith);
434422
}
435423

436424
private class FilteringUrlEnum implements Enumeration<URL> {

0 commit comments

Comments
 (0)