Skip to content

Commit 95ab859

Browse files
enh: uses packagingExclude / include for war:exploded (#555)
* enh: uses packagingExclude / include parameters for all goals, not just war:war goal * Add simple IT --------- Co-authored-by: Slawomir Jaranowski <s.jaranowski@gmail.com>
1 parent cac8186 commit 95ab859

File tree

8 files changed

+232
-57
lines changed

8 files changed

+232
-57
lines changed

src/it/gh-554/invoker.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
invoker.goals=war:exploded

src/it/gh-554/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<project 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' xmlns='http://maven.apache.org/POM/4.0.0'>
21+
<modelVersion>4.0.0</modelVersion>
22+
<groupId>testwar</groupId>
23+
<artifactId>gh-554</artifactId>
24+
<packaging>war</packaging>
25+
<version>1.0-SNAPSHOT</version>
26+
<name>gh-554 Maven Webapp</name>
27+
<url>https://github.com/apache/maven-war-plugin/issues/554</url>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.apache.commons</groupId>
32+
<artifactId>commons-lang3</artifactId>
33+
<version>3.19.0</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>commons-io</groupId>
37+
<artifactId>commons-io</artifactId>
38+
<version>2.20.0</version>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<artifactId>maven-war-plugin</artifactId>
46+
<version>@project.version@</version>
47+
<configuration>
48+
<packagingExcludes>WEB-INF/lib/*io*.jar</packagingExcludes>
49+
</configuration>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE web-app PUBLIC
2+
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3+
"http://java.sun.com/dtd/web-app_2_3.dtd" >
4+
<!--
5+
Licensed to the Apache Software Foundation (ASF) under one
6+
or more contributor license agreements. See the NOTICE file
7+
distributed with this work for additional information
8+
regarding copyright ownership. The ASF licenses this file
9+
to you under the Apache License, Version 2.0 (the
10+
"License"); you may not use this file except in compliance
11+
with the License. You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing,
16+
software distributed under the License is distributed on an
17+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18+
KIND, either express or implied. See the License for the
19+
specific language governing permissions and limitations
20+
under the License.
21+
-->
22+
<web-app>
23+
<display-name>Archetype Created Web Application</display-name>
24+
</web-app>

src/it/gh-554/verify.groovy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
def libDirectory = new File(basedir, 'target/gh-554-1.0-SNAPSHOT/WEB-INF/lib');
21+
22+
// only one dependency should be copied
23+
assert libDirectory.list().length == 1

src/main/java/org/apache/maven/plugins/war/AbstractWarMojo.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,26 @@ public abstract class AbstractWarMojo extends AbstractMojo {
230230
@Parameter
231231
private String dependentWarExcludes = StringUtils.join(Overlay.DEFAULT_EXCLUDES, ",");
232232

233+
/**
234+
* The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement
235+
* the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude
236+
* specific pattern using the expression %regex[]. Hint: read the about (?!Pattern).
237+
*
238+
* @since 2.1-alpha-2
239+
*/
240+
@Parameter(property = "maven.war.packagingExcludes")
241+
private String packagingExcludes;
242+
243+
/**
244+
* The comma separated list of tokens to include in the WAR before packaging. By default everything is included.
245+
* This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions
246+
* engine to include and exclude specific pattern using the expression %regex[].
247+
*
248+
* @since 2.1-beta-1
249+
*/
250+
@Parameter
251+
private String packagingIncludes;
252+
233253
/**
234254
* The overlays to apply. Each &lt;overlay&gt; element may contain:
235255
* <ul>
@@ -826,6 +846,24 @@ public void deleteOutdatedResources() {
826846
public String getOutputTimestamp() {
827847
return outputTimestamp;
828848
}
849+
850+
/**
851+
* @return list of packaging excludes.
852+
* @since 3.4.1
853+
*/
854+
@Override
855+
public List<String> getPackagingExcludes() {
856+
return Arrays.asList(AbstractWarMojo.this.getPackagingExcludes());
857+
}
858+
859+
/**
860+
* @return list of packaging includes.
861+
* @since 3.4.1
862+
*/
863+
@Override
864+
public List<String> getPackagingIncludes() {
865+
return Arrays.asList(AbstractWarMojo.this.getPackagingIncludes());
866+
}
829867
}
830868

831869
/**
@@ -1044,4 +1082,40 @@ protected boolean isRecompressZippedFiles() {
10441082
protected boolean isIncludeEmptyDirectories() {
10451083
return includeEmptyDirectories;
10461084
}
1085+
1086+
/**
1087+
* @return The package excludes.
1088+
*/
1089+
public String[] getPackagingExcludes() {
1090+
if (packagingExcludes == null || packagingExcludes.isEmpty()) {
1091+
return new String[0];
1092+
} else {
1093+
return org.codehaus.plexus.util.StringUtils.split(packagingExcludes, ",");
1094+
}
1095+
}
1096+
1097+
/**
1098+
* @param packagingExcludes {@link #packagingExcludes}
1099+
*/
1100+
public void setPackagingExcludes(String packagingExcludes) {
1101+
this.packagingExcludes = packagingExcludes;
1102+
}
1103+
1104+
/**
1105+
* @return The packaging includes.
1106+
*/
1107+
public String[] getPackagingIncludes() {
1108+
if (packagingIncludes == null || packagingIncludes.isEmpty()) {
1109+
return new String[] {"**"};
1110+
} else {
1111+
return org.codehaus.plexus.util.StringUtils.split(packagingIncludes, ",");
1112+
}
1113+
}
1114+
1115+
/**
1116+
* @param packagingIncludes {@link #packagingIncludes}
1117+
*/
1118+
public void setPackagingIncludes(String packagingIncludes) {
1119+
this.packagingIncludes = packagingIncludes;
1120+
}
10471121
}

src/main/java/org/apache/maven/plugins/war/WarMojo.java

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.codehaus.plexus.archiver.manager.ArchiverManager;
5252
import org.codehaus.plexus.archiver.war.WarArchiver;
5353
import org.codehaus.plexus.util.FileUtils;
54-
import org.codehaus.plexus.util.StringUtils;
5554

5655
/**
5756
* Build a WAR file.
@@ -83,26 +82,6 @@ public class WarMojo extends AbstractWarMojo {
8382
@Parameter
8483
private String classifier;
8584

86-
/**
87-
* The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement
88-
* the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude
89-
* specific pattern using the expression %regex[]. Hint: read the about (?!Pattern).
90-
*
91-
* @since 2.1-alpha-2
92-
*/
93-
@Parameter(property = "maven.war.packagingExcludes")
94-
private String packagingExcludes;
95-
96-
/**
97-
* The comma separated list of tokens to include in the WAR before packaging. By default everything is included.
98-
* This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions
99-
* engine to include and exclude specific pattern using the expression %regex[].
100-
*
101-
* @since 2.1-beta-1
102-
*/
103-
@Parameter
104-
private String packagingIncludes;
105-
10685
/**
10786
* The WAR archiver.
10887
*/
@@ -385,42 +364,6 @@ public void setClassifier(String classifier) {
385364
this.classifier = classifier;
386365
}
387366

388-
/**
389-
* @return The package excludes.
390-
*/
391-
public String[] getPackagingExcludes() {
392-
if (packagingExcludes == null || packagingExcludes.isEmpty()) {
393-
return new String[0];
394-
} else {
395-
return StringUtils.split(packagingExcludes, ",");
396-
}
397-
}
398-
399-
/**
400-
* @param packagingExcludes {@link #packagingExcludes}
401-
*/
402-
public void setPackagingExcludes(String packagingExcludes) {
403-
this.packagingExcludes = packagingExcludes;
404-
}
405-
406-
/**
407-
* @return The packaging includes.
408-
*/
409-
public String[] getPackagingIncludes() {
410-
if (packagingIncludes == null || packagingIncludes.isEmpty()) {
411-
return new String[] {"**"};
412-
} else {
413-
return StringUtils.split(packagingIncludes, ",");
414-
}
415-
}
416-
417-
/**
418-
* @param packagingIncludes {@link #packagingIncludes}
419-
*/
420-
public void setPackagingIncludes(String packagingIncludes) {
421-
this.packagingIncludes = packagingIncludes;
422-
}
423-
424367
/**
425368
* @return {@link #outputDirectory}
426369
*/

src/main/java/org/apache/maven/plugins/war/packaging/AbstractWarPackagingTask.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.nio.file.Files;
2424
import java.nio.file.attribute.BasicFileAttributes;
25+
import java.util.List;
2526

2627
import org.apache.commons.io.input.XmlStreamReader;
2728
import org.apache.maven.artifact.Artifact;
@@ -38,6 +39,7 @@
3839
import org.codehaus.plexus.interpolation.InterpolationException;
3940
import org.codehaus.plexus.util.DirectoryScanner;
4041
import org.codehaus.plexus.util.FileUtils;
42+
import org.codehaus.plexus.util.SelectorUtils;
4143

4244
/**
4345
* @author Stephane Nicoll
@@ -149,6 +151,10 @@ protected void copyFile(String sourceId, final WarPackagingContext context, fina
149151
throws IOException
150152
// CHECKSTYLE_ON: LineLength
151153
{
154+
if (isExcluded(targetFilename, context.getPackagingIncludes(), context.getPackagingExcludes())) {
155+
context.getLog().debug("Skipping excluded file: " + targetFilename);
156+
return;
157+
}
152158
final File targetFile = new File(context.getWebappDirectory(), targetFilename);
153159

154160
if (file.isFile()) {
@@ -455,4 +461,26 @@ private boolean isPropertiesFile(File file) {
455461
private boolean isXmlFile(File file) {
456462
return isFileOfType(file, ".xml");
457463
}
464+
465+
/**
466+
* Check whether the specified file is excluded or not.
467+
*
468+
* @param targetFilename the target filename
469+
* @param packagingIncludes the includes
470+
* @param packagingExcludes the excludes
471+
* @return true if the file is excluded
472+
*/
473+
private boolean isExcluded(String targetFilename, List<String> packagingIncludes, List<String> packagingExcludes) {
474+
for (String exclude : packagingExcludes) {
475+
if (SelectorUtils.matchPath(exclude.trim(), targetFilename)) {
476+
return true;
477+
}
478+
}
479+
for (String include : packagingIncludes) {
480+
if (SelectorUtils.matchPath(include.trim(), targetFilename)) {
481+
return false;
482+
}
483+
}
484+
return true;
485+
}
458486
}

src/main/java/org/apache/maven/plugins/war/packaging/WarPackagingContext.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,16 @@ public interface WarPackagingContext {
256256
* @since 3.3.0
257257
*/
258258
String getOutputTimestamp();
259+
260+
/**
261+
* @return list of packaging excludes.
262+
* @since 3.4.1
263+
*/
264+
List<String> getPackagingExcludes();
265+
266+
/**
267+
* @return list of packaging includes.
268+
* @since 3.4.1
269+
*/
270+
List<String> getPackagingIncludes();
259271
}

0 commit comments

Comments
 (0)