Skip to content

Commit

Permalink
Merge pull request #161 from rmannibucau/160_child-first-classloader-…
Browse files Browse the repository at this point in the history
…with-exclusion-support

Fixes #160, ensure the java classloader is a child first one and supports to excludes some gathered classpath element to solve manually conflicts
  • Loading branch information
olamy authored Jul 23, 2020
2 parents 81e42e7 + 0d9cc56 commit 94570cc
Show file tree
Hide file tree
Showing 9 changed files with 809 additions and 41 deletions.
52 changes: 52 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,17 @@
<version>1.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>

<properties>
<mavenVersion>3.0</mavenVersion>

<slf4j.version>1.7.30</slf4j.version>
</properties>

<build>
Expand Down Expand Up @@ -269,6 +276,51 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-test-deps</id>
<phase>generate-test-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<type>jar</type>
<overWrite>false</overWrite>
<destFileName>slf4j-api.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<type>jar</type>
<overWrite>false</overWrite>
<destFileName>slf4j-simple.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4j.version}</version>
<type>jar</type>
<overWrite>false</overWrite>
<destFileName>slf4j-jdk14.jar</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/test-dependencies/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
33 changes: 30 additions & 3 deletions src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -183,6 +184,15 @@ public class ExecJavaMojo
@Parameter
private List<String> additionalClasspathElements;

/**
* List of file to exclude from the classpath.
* It matches the jar name, for example {@code slf4j-simple-1.7.30.jar}.
*
* @since 3.0.1
*/
@Parameter
private List<String> classpathFilenameExclusions;

/**
* Execute goal.
*
Expand Down Expand Up @@ -270,7 +280,8 @@ public void run()
}
}
}, mainClass + ".main()" );
bootstrapThread.setContextClassLoader( getClassLoader() );
URLClassLoader classLoader = getClassLoader();
bootstrapThread.setContextClassLoader( classLoader );
setSystemProperties();

bootstrapThread.start();
Expand Down Expand Up @@ -298,6 +309,18 @@ public void run()
}
}

if ( classLoader != null )
{
try
{
classLoader.close();
}
catch ( IOException e )
{
getLog().error(e.getMessage(), e);
}
}

if ( originalSystemProperties != null )
{
System.setProperties( originalSystemProperties );
Expand Down Expand Up @@ -485,7 +508,7 @@ private void setSystemProperties()
* @return the classloader
* @throws MojoExecutionException if a problem happens
*/
private ClassLoader getClassLoader()
private URLClassLoader getClassLoader()
throws MojoExecutionException
{
List<Path> classpathURLs = new ArrayList<>();
Expand All @@ -495,7 +518,11 @@ private ClassLoader getClassLoader()

try
{
return LoaderFinder.find( classpathURLs, mainClass );
return URLClassLoaderBuilder.builder()
.setLogger( getLog() )
.setPaths( classpathURLs )
.setExclusions( classpathFilenameExclusions )
.build();
}
catch ( NullPointerException | IOException e )
{
Expand Down
34 changes: 0 additions & 34 deletions src/main/java/org/codehaus/mojo/exec/LoaderFinder.java

This file was deleted.

Loading

0 comments on commit 94570cc

Please sign in to comment.