Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,21 @@ public interface ParsedPomFile {

/**
* Returns a list of files defined as a resources for current project.
* This method is not able to handle hierarchical resources and includes/excludes
*
* @see ParsedPomFile#getResources()
* @return
*/
@Deprecated
List<File> getProjectResources();

/**
* Returns a list of defined resources for current project.
*
* @return
*/
List<Resource> getResources();

/**
* Returns interpolated properties defined in the current project
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.shrinkwrap.resolver.api.maven.pom;

import java.io.File;

/**
* Represents a file-backed resource used by Maven model.
*
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
*
*/
public class Resource {

private final File source;

private final String targetPath;

public Resource(File source, String targetPath) {
this.source = source;
this.targetPath = targetPath;
}

/**
* Return a file that contains content of the resource
*
* @return
*/
public File getSource() {
return source;
}

/**
* Returns a normalized path where resource should be stored in archive
*
* @return
*/
public String getTargetPath() {
return targetPath;
}

}
63 changes: 63 additions & 0 deletions impl-maven-archive/src/it/jar-sample/pom-c.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 Importer Implementation Tests: Jar Sample B</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>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/main.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</build>

</project>
2 changes: 1 addition & 1 deletion impl-maven-archive/src/it/war-sample/pom-b.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
</manifestEntries>
</archive>
<packagingIncludes>
**/*.packagingToExclude
**/*.packagingToExclude,**/*.jar
</packagingIncludes>
<warSourceIncludes>
**/*.packagingToExclude,**/*.warSourceToExclude,**/web.xml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
property=value
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.shrinkwrap.resolver.impl.maven.archive.packaging;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.util.SelectorUtils;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.Filter;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.ShrinkWrap;

/**
* Utils related to filtering of archive content
*
* @author <a href="mailto:kpiwko@redhat.com">Karel Piwko</a>
*
*/
public class ArchiveFilteringUtils {

public static <T extends Archive<?>> T filterArchiveContent(T archive, Class<T> archiveType, final String[] includes,
final String[] excludes) {
return filterArchiveContent(archive, archiveType, Arrays.asList(includes), Arrays.asList(excludes));
}

public static <T extends Archive<?>> T filterArchiveContent(T archive, Class<T> archiveType, final List<String> includes,
final List<String> excludes) {

// get all files that should be included in archive
Map<ArchivePath, Node> includePart = archive.getContent(new Filter<ArchivePath>() {
@Override
public boolean include(ArchivePath path) {

// trim first slash
String pathAsString = path.get();
pathAsString = pathAsString.startsWith("/") ? pathAsString.substring(1) : pathAsString;

boolean include = false;
// include all files that should be included
includesLoop: for (String i : includes) {
if (SelectorUtils.matchPath(i, pathAsString, true)) {
// if file should be included, check also for excludes
for (String e : excludes) {
if (SelectorUtils.matchPath(e, pathAsString, true)) {
break includesLoop;
}
}
include = true;
break;
}
}

return include;
}
});

// create new archive and merge content together
T newArchive = ShrinkWrap.create(archiveType, archive.getName());

for (Map.Entry<ArchivePath, Node> entry : includePart.entrySet()) {
if (entry.getValue() != null && entry.getValue().getAsset() != null) {
newArchive.add(entry.getValue().getAsset(), entry.getKey());
}
}

return newArchive;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
*/
package org.jboss.shrinkwrap.resolver.impl.maven.archive.packaging;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Manifest;

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 All @@ -31,6 +27,7 @@
import org.jboss.shrinkwrap.resolver.api.maven.PackagingType;
import org.jboss.shrinkwrap.resolver.api.maven.ScopeType;
import org.jboss.shrinkwrap.resolver.api.maven.pom.ParsedPomFile;
import org.jboss.shrinkwrap.resolver.api.maven.pom.Resource;
import org.jboss.shrinkwrap.resolver.api.maven.strategy.MavenResolutionStrategy;
import org.jboss.shrinkwrap.resolver.impl.maven.archive.plugins.JarPluginConfiguration;
import org.jboss.shrinkwrap.resolver.impl.maven.util.Validate;
Expand Down Expand Up @@ -81,19 +78,18 @@ public JarPackagingProcessor importBuildOutput(MavenResolutionStrategy strategy)
JarPluginConfiguration jarConfiguration = new JarPluginConfiguration(pomFile);

// add resources
final ListFilter listFilter = new ListFilter();
listFilter.setIncludes(jarConfiguration.getIncludes());
listFilter.setExcludes(jarConfiguration.getExcludes());
listFilter.addDefaultExcludes();

for (File resource : listFilter.scan(pomFile.getProjectResources(), pomFile.getBaseDirectory())) {
archive.addAsResource(resource);
for (Resource resource : pomFile.getResources()) {
archive.addAsResource(resource.getSource(), resource.getTargetPath());
}

// set manifest
Manifest manifest = jarConfiguration.getArchiveConfiguration().asManifest();
archive.setManifest(new ManifestAsset(manifest));

// construct new archive via applying includes/excludes
archive = ArchiveFilteringUtils.filterArchiveContent(archive, JavaArchive.class, jarConfiguration.getIncludes(),
jarConfiguration.getExcludes());

return this;
}

Expand All @@ -102,44 +98,4 @@ 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.jboss.shrinkwrap.resolver.api.maven.PackagingType;
import org.jboss.shrinkwrap.resolver.api.maven.ScopeType;
import org.jboss.shrinkwrap.resolver.api.maven.pom.ParsedPomFile;
import org.jboss.shrinkwrap.resolver.api.maven.pom.Resource;
import org.jboss.shrinkwrap.resolver.api.maven.strategy.MavenResolutionStrategy;
import org.jboss.shrinkwrap.resolver.impl.maven.archive.plugins.WarPluginConfiguration;
import org.jboss.shrinkwrap.resolver.impl.maven.task.AddAllDeclaredDependenciesTask;
Expand Down Expand Up @@ -91,8 +92,8 @@ public WarPackagingProcessor importBuildOutput(MavenResolutionStrategy strategy)
}

// add resources
for (File resource : pomFile.getProjectResources()) {
archive.addAsResource(resource);
for (Resource resource : pomFile.getResources()) {
archive.addAsResource(resource.getSource(), resource.getTargetPath());
}

WarPluginConfiguration warConfiguration = new WarPluginConfiguration(pomFile);
Expand All @@ -115,6 +116,10 @@ public WarPackagingProcessor importBuildOutput(MavenResolutionStrategy strategy)
Manifest manifest = warConfiguration.getArchiveConfiguration().asManifest();
archive.setManifest(new ManifestAsset(manifest));

// filter via includes/excludes
archive = ArchiveFilteringUtils.filterArchiveContent(archive, WebArchive.class, warConfiguration.getIncludes(),
warConfiguration.getExcludes());

return this;
}

Expand Down Expand Up @@ -146,7 +151,6 @@ public boolean include(ArchivePath archivePath) {
* @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);
Expand Down
Loading