Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#68 need classpathDependencyExcludes support #69

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-common-artifact-filters</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>org.apache.maven.shared</groupId>
Expand Down
53 changes: 51 additions & 2 deletions src/main/java/org/codehaus/mojo/exec/AbstractExecMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@
*/

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Resource;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
import org.codehaus.plexus.util.cli.CommandLineUtils;

/**
Expand Down Expand Up @@ -140,6 +144,17 @@ public abstract class AbstractExecMojo
*/
@Parameter( property = "addOutputToClasspath", defaultValue = "true" )
private boolean addOutputToClasspath;


/**
* List of dependencies to exclude from the test classpath. Each dependency string must follow the format
* <i>groupId:artifactId</i>. For example: <i>org.acme:project-a</i>
*
* @since 1.5.1
*/
@Parameter( property = "exec.classpathDependencyExcludes" )
private String[] classpathDependencyExcludes;


/**
* Collects the project artifacts in the specified List and the project specific classpath (build output and build
Expand Down Expand Up @@ -192,10 +207,44 @@ else if ( "system".equals( classpathScope ) )
{
throw new IllegalStateException( "Invalid classpath scope: " + classpathScope );
}

getLog().debug( "Collected project artifacts " + artifacts );
//filter dependency
if ( classpathDependencyExcludes != null )
{
ArtifactFilter dependencyFilter =
new PatternIncludesArtifactFilter( Arrays.asList( classpathDependencyExcludes ) );
List<Artifact> filterList = this.filterArtifacts( artifacts, dependencyFilter );
artifacts.clear();
artifacts.addAll(filterList);
getLog().debug( "Collected project artifacts after filter " + artifacts );
}


getLog().debug( "Collected project classpath " + theClasspathFiles );
}

/**
* Return a new set containing only the artifacts accepted by the given filter.
*
* @param artifacts The unfiltered artifacts
* @param filter The filter to apply
* @return The filtered result
*/
private List<Artifact> filterArtifacts( List<Artifact> artifacts, ArtifactFilter filter )
{
List<Artifact> filteredArtifacts = new ArrayList<Artifact>();

for ( Artifact artifact : artifacts )
{
if ( !filter.include( artifact ) )
{
filteredArtifacts.add( artifact );
}
}

return filteredArtifacts;
}

/**
* Parses the argument string given by the user. Strings are recognized as everything between STRING_WRAPPER.
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/org/codehaus/mojo/exec/ExecJavaMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.File;
import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
Expand Down Expand Up @@ -135,6 +137,32 @@ public void testWaitNoDaemonThreads()

assertEquals( MainWithThreads.ALL_EXITED, output.trim() );
}



public void testClasspathDependencyExcludes()
throws Exception
{
File pom = new File( getBasedir(), "src/test/projects/project15/pom.xml" );
String output = executeDebug( pom, "java" );
String expectedExcludeDependency = "commons-logging:commons-logging";

Pattern pattern = Pattern.compile(".*?Collected project artifacts \\[(.*?)\\].*");
Matcher matcher = pattern.matcher(output.trim());
String result = expectedExcludeDependency;
//check it present in project dependency
if(matcher.find()){
pattern = Pattern.compile(".*?Collected project artifacts after filter \\[(.*?)\\].*");
matcher = pattern.matcher(output.trim());
//check it present after filter
if(matcher.find()){
result = matcher.group(0);
}
}

assertFalse(result.contains(expectedExcludeDependency) );
}


/**
* For cases where the Java code spawns Threads and main returns soon, but code contains non interruptible threads.
Expand Down Expand Up @@ -235,6 +263,52 @@ private String execute( File pom, String goal )

return stringOutputStream.toString();
}


/**
* @return output from System.out during mojo execution
*/
private String executeDebug( File pom, String goal )
throws Exception
{

ExecJavaMojo mojo;
mojo = (ExecJavaMojo) lookupMojo( goal, pom );

setUpProject( pom, mojo );

MavenProject project = (MavenProject) getVariableValueFromObject( mojo, "project" );

// why isn't this set up by the harness based on the default-value? TODO get to bottom of this!
setVariableValueToObject( mojo, "includeProjectDependencies", Boolean.TRUE );
setVariableValueToObject( mojo, "killAfter", (long) -1 );
setVariableValueToObject( mojo, "cleanupDaemonThreads", Boolean.TRUE );
setVariableValueToObject( mojo, "classpathScope", "compile" );

assertNotNull( mojo );
assertNotNull( project );

// trap System.out
PrintStream out = System.out;
StringOutputStream stringOutputStream = new StringOutputStream();
System.setOut( new PrintStream( stringOutputStream ) );
// ensure we don't log unnecessary stuff which would interfere with assessing success of tests
mojo.setLog( new DefaultLog( new ConsoleLogger( Logger.LEVEL_DEBUG, "exec:java" ) ) );

try
{
mojo.execute();
}
finally
{
// see testUncooperativeThread() for explaination
Thread.sleep( 300 ); // time seems about right
System.setOut( out );
}

return stringOutputStream.toString();
}


private void setUpProject( File pomFile, AbstractMojo mojo )
throws Exception
Expand Down
88 changes: 88 additions & 0 deletions src/test/projects/project15/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.cb.maven.plugins.exec</groupId>
<artifactId>project15</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>Maven Exec Plugin</name>
<description>Test that one can force adding the test-classpath for the java mojo</description>

<inceptionYear>2005</inceptionYear>
<licenses>
<license>
<name>Apache License 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<developers>
<developer>
<name>Jerome Lacoste</name>
<id>jerome</id>
<email>[email protected]</email>
<organization>CoffeeBreaks</organization>
<organizationUrl>http://www.coffeebreaks.org</organizationUrl>
<roles>
<role>Java Developer</role>
</roles>
<timezone>+1</timezone>
</developer>
</developers>

<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<!-- for manual tests. Can't be automated in the unit tests as the plugin's not installed. What about integration tests? -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>true</includeProjectDependencies>
<classpathDependencyExcludes>
<classpathDependencyExclude>commons-logging:commons-logging</classpathDependencyExclude>
</classpathDependencyExcludes>
<mainClass>org.codehaus.mojo.exec.DummyMain</mainClass>
<commandlineArgs>-X</commandlineArgs>
<arguments>
<argument>-X</argument>
</arguments>
</configuration>
</plugin>
</plugins>
<!--extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh-external</artifactId>
<version>1.0-alpha-5-SNAPSHOT</version>
</extension>
</extensions-->
</build>

</project>