Skip to content

Commit

Permalink
SHRINKRES-62 MavenImporter should support shrinkwrap filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernard Labno authored and ALRubinger committed Feb 27, 2013
1 parent ce53ae2 commit 9849c92
Show file tree
Hide file tree
Showing 15 changed files with 442 additions and 10 deletions.
61 changes: 61 additions & 0 deletions impl-maven-archive/src/it/jar-sample/pom-b.xml
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<!-- Model Version -->
<modelVersion>4.0.0</modelVersion>

<!-- Artifact Configuration -->
<groupId>org.jboss.shrinkwrap.resolver.test</groupId>
<artifactId>shrinkwrap-resolver-impl-maven-test-jar-sample</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>ShrinkWrap Resolver Maven Archie Implementation Tests: Jar Sample</name>

<dependencies>
<!-- External Projects -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.7</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.10</version>
</dependency>

<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-web-6.0</artifactId>
<version>3.0.2.Final</version>
<scope>provided</scope>
<type>pom</type>
<exclusions>
<!-- xalan is not in Maven Central -->
<exclusion>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<includes>
**/*.toExclude
</includes>
</configuration>
</plugin>
</plugins>
</build>

</project>
12 changes: 12 additions & 0 deletions impl-maven-archive/src/it/jar-sample/pom.xml
Expand Up @@ -44,6 +44,18 @@
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<excludes>
**/*.toExclude
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
Empty file.
105 changes: 105 additions & 0 deletions impl-maven-archive/src/it/war-sample/pom-b.xml
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<!-- Model Version -->
<modelVersion>4.0.0</modelVersion>

<!-- Artifact Configuration -->
<groupId>org.jboss.shrinkwrap.resolver.test</groupId>
<artifactId>shrinkwrap-resolver-impl-maven-test-war-sample</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>ShrinkWrap Resolver Maven Archive Implementation Tests: War Sample</name>

<dependencies>
<!-- External Projects -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.7</version>
</dependency>

<!-- Manifest dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.spec</groupId>
<artifactId>jboss-javaee-web-6.0</artifactId>
<version>3.0.2.Final</version>
<scope>provided</scope>
<type>pom</type>
<exclusions>
<!-- xalan is not in Maven Central -->
<exclusion>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.10</version>
</dependency>

</dependencies>

<build>
<finalName>war-sample</finalName>
<plugins>
<!-- configure war-plugin to generate manifest with Dependencies: org.slf4j -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<archive>
<manifestEntries>
<Dependencies>org.slf4j</Dependencies>
</manifestEntries>
</archive>
<packagingIncludes>
**/*.packagingToExclude
</packagingIncludes>
<warSourceIncludes>
**/*.warSourceToExclude
</warSourceIncludes>
<includes>
**/*.toExclude
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<verbose>false</verbose>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
9 changes: 9 additions & 0 deletions impl-maven-archive/src/it/war-sample/pom.xml
Expand Up @@ -66,6 +66,15 @@
<Dependencies>org.slf4j</Dependencies>
</manifestEntries>
</archive>
<packagingExcludes>
**/*.packagingToExclude
</packagingExcludes>
<warSourceExcludes>
**/*.warSourceToExclude
</warSourceExcludes>
<excludes>
**/*.toExclude
</excludes>
</configuration>
</plugin>
<plugin>
Expand Down
Empty file.
Empty file.
Empty file.
Expand Up @@ -17,8 +17,10 @@
package org.jboss.shrinkwrap.resolver.impl.maven.archive.packaging;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -27,6 +29,7 @@
import org.codehaus.plexus.compiler.CompilerMessage;
import org.codehaus.plexus.compiler.CompilerResult;
import org.codehaus.plexus.compiler.javac.JavacCompiler;
import org.codehaus.plexus.util.DirectoryScanner;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolvedArtifact;
import org.jboss.shrinkwrap.resolver.api.maven.MavenWorkingSession;
Expand All @@ -53,8 +56,20 @@ public abstract class AbstractCompilingProcessor<ARCHIVETYPE extends Archive<ARC
public static final String MAVEN_COMPILER_SOURCE_VERSION = "1.5";
public static final String MAVEN_COMPILER_TARGET_VERSION = "1.5";

public static final String[] DEFAULT_INCLUDES = {"**/**"};

protected MavenWorkingSession session;

protected static void addTokenized(Map<String, Object> warConfiguration, ArrayList<String> excludes, String configurationKey) {
final Object packagingExcludes = warConfiguration.get(configurationKey);
if (packagingExcludes != null) {
final StringTokenizer tokenizer = new StringTokenizer(packagingExcludes.toString(), ",");
while (tokenizer.hasMoreElements()) {
excludes.add(tokenizer.nextToken());
}
}
}

protected PackagingProcessor<ARCHIVETYPE> configure(MavenWorkingSession session) {
this.session = session;
return this;
Expand Down Expand Up @@ -151,4 +166,51 @@ private static MavenImporterException constructCompilationException(CompilerResu

}

protected String[] getExcludes(Map<String, Object> configuration) {
final ArrayList<String> excludes = new ArrayList<String>();
addTokenized(configuration, excludes, "excludes");
return excludes.toArray(new String[excludes.size()]);
}

protected String[] getIncludes(Map<String, Object> configuration) {
final ArrayList<String> excludes = new ArrayList<String>();
addTokenized(configuration, excludes, "includes");
return excludes.toArray(new String[excludes.size()]);
}

/**
* Returns the file to copy. If the includes are <tt>null</tt> or empty, the
* default includes are used.
*
* @param baseDir the base directory to start from
* @param includes the includes
* @param excludes the excludes
* @return the files to copy
*/
protected String[] getFilesToIncludes(File baseDir, String[] includes, String[] excludes) {
final DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(baseDir);

if (excludes != null) {
scanner.setExcludes(excludes);
}
scanner.addDefaultExcludes();

if (includes != null && includes.length > 0) {
scanner.setIncludes(includes);
} else {
scanner.setIncludes(DEFAULT_INCLUDES);
}

scanner.scan();

final String[] includedFiles = scanner.getIncludedFiles();
for (int i = 0; i < includedFiles.length; i++) {
includedFiles[i] = "/" + includedFiles[i];
}
return includedFiles;

}


}
Expand Up @@ -17,7 +17,11 @@
package org.jboss.shrinkwrap.resolver.impl.maven.archive.packaging;

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

import org.codehaus.plexus.util.AbstractScanner;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.importer.ExplodedImporter;
Expand Down Expand Up @@ -75,8 +79,18 @@ public JarPackagingProcessor importBuildOutput(MavenResolutionStrategy strategy)
archive = archive.merge(classes);
}

final Map<String, Object> jarConfiguration = pomFile.getPluginConfiguration(MAVEN_WAR_PLUGIN_KEY);
// add resources
for (File resource : pomFile.getProjectResources()) {
final ListFilter listFilter = new ListFilter();
final String[] includes = getIncludes(jarConfiguration);
listFilter.setExcludes(getExcludes(jarConfiguration));
listFilter.addDefaultExcludes();
if (includes == null || includes.length == 0) {
listFilter.setIncludes(DEFAULT_INCLUDES);
} else {
listFilter.setIncludes(includes);
}
for (File resource : listFilter.scan(pomFile.getProjectResources(), pomFile.getBaseDirectory())) {
archive.addAsResource(resource);
}

Expand All @@ -88,4 +102,44 @@ public JavaArchive getResultingArchive() {
return archive;
}

private class ListFilter extends AbstractScanner {
public List<File> scan(Iterable<File> newfiles, File root) {
setupDefaultFilters();

final List<File> includedFiles = new ArrayList<File>();
final int rootPathLength = root.getAbsolutePath().length();

for (File file : newfiles) {
String name = file.getAbsolutePath().substring(rootPathLength + 1);
if (file.isFile()) {
if (isIncluded(name)) {
if (!isExcluded(name)) {
includedFiles.add(file);
}
}
}
}
return includedFiles;
}

@Override
public void scan() {
throw new UnsupportedOperationException();
}

@Override
public String[] getIncludedFiles() {
throw new UnsupportedOperationException();
}

@Override
public String[] getIncludedDirectories() {
throw new UnsupportedOperationException();
}

@Override
public File getBasedir() {
throw new UnsupportedOperationException();
}
}
}

0 comments on commit 9849c92

Please sign in to comment.