Skip to content

Commit 5ed7d90

Browse files
authored
[ARCHETYPE-274] Conditionally include or exclude a file from archetype during generation (apache#244)
1 parent e3e970b commit 5ed7d90

File tree

41 files changed

+432
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+432
-27
lines changed

NOTICE.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
This product includes software developed by
22
The Apache Software Foundation (http://www.apache.org/).
3-

archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ public void createArchetype(ArchetypeCreationRequest request, ArchetypeCreationR
183183
Model pom = pomManager.readPom(project.getFile());
184184

185185
List<String> excludePatterns = configurationProperties.getProperty(Constants.EXCLUDE_PATTERNS) != null
186-
? Arrays.asList(
187-
StringUtils.split(configurationProperties.getProperty(Constants.EXCLUDE_PATTERNS), ","))
186+
? Arrays.asList(configurationProperties
187+
.getProperty(Constants.EXCLUDE_PATTERNS)
188+
.split(","))
188189
: Collections.emptyList();
189190

190191
List<String> fileNames = resolveFileNames(pom, basedir, excludePatterns);

archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,17 @@ private void processTemplates(
780780
FileSet fileSet = iterator.next();
781781
count++;
782782

783+
final String includeCondition = fileSet.getIncludeCondition();
784+
if (includeCondition != null && !includeCondition.isEmpty()) {
785+
final String evaluatedCondition = evaluateExpression(context, "includeCondition", includeCondition);
786+
if (!Boolean.parseBoolean(evaluatedCondition)) {
787+
LOGGER.debug(String.format(
788+
"Skipping fileset %s due to includeCondition: %s being: %s",
789+
fileSet, includeCondition, evaluatedCondition));
790+
continue;
791+
}
792+
}
793+
783794
List<String> fileSetResources =
784795
archetypeFilesResolver.filterFiles(moduleOffset, fileSet, archetypeResources);
785796

archetype-common/src/test/archetypes/basic-1.0/archetype-resources/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@
4040
<!-- empty dependencies, with comments -->
4141
</dependencies>
4242

43-
</project>
43+
</project>

archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@
3535
<module>subproject</module>
3636
</modules>
3737

38-
</project>
38+
</project>

archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@
4141
<module>subsubproject</module>
4242
</modules>
4343

44-
</project>
44+
</project>

archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@
3737
<name>Maven archetype Test Subsubfolder</name>
3838
<packaging>jar</packaging>
3939

40-
</project>
40+
</project>

archetype-common/src/test/archetypes/old-1.0/archetype-resources/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@
3636
<!-- empty dependencies, with comments -->
3737
</dependencies>
3838

39-
</project>
39+
</project>

archetype-common/src/test/archetypes/partial-1.0/archetype-resources/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@
5858
</plugin>
5959
</plugins>
6060
</reporting>
61-
</project>
61+
</project>

archetype-common/src/test/repository/org/apache/maven/archetypes/maven-archetype-quickstart/1.0-alpha-1-SNAPSHOT/maven-archetype-quickstart-1.0-alpha-1-SNAPSHOT.pom

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
<modelVersion>4.0.0</modelVersion>
2424
<artifactId>maven-archetype-quickstart</artifactId>
2525
<version>1.0-alpha-1-SNAPSHOT</version>
26-
</project>
26+
</project>

archetype-models/archetype-descriptor/src/main/mdo/archetype-descriptor.mdo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@
162162
<required>false</required>
163163
<description>Encoding to use when filtering content.</description>
164164
</field>
165+
<field xml.attribute="true">
166+
<name>includeCondition</name>
167+
<version>1.1.0+</version>
168+
<type>String</type>
169+
<required>false</required>
170+
<description>A string value that should resolve to a boolean value to conditionally include filesets.
171+
This condition should be either a boolean as String or a velocity template language statement that resolves
172+
to a boolean value. If the descriptor contains includeCondition="${shouldInclude}" and the archetype has
173+
a (required) property like shouldInclude=true the fileset is included.</description>
174+
</field>
165175
<field>
166176
<name>directory</name>
167177
<version>1.0.0+</version>

archetype-samples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
<archetype.properties>archetype.properties</archetype.properties>
2727
</properties>
2828

29-
</project>
29+
</project>

deploySite.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@
2121

2222
mvn package site site:stage -Preporting $@
2323
mvn scm-publish:publish-scm $@
24-

maven-archetype-plugin/src/it/projects/ARCHETYPE-241_filter-directory/archetype/src/main/resources/archetype-resources/__projectName__-test2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ under the License.
3030
</parent>
3131
<artifactId>${projectName}-test2</artifactId>
3232

33-
</project>
33+
</project>

maven-archetype-plugin/src/it/projects/ARCHETYPE-241_filter-directory/archetype/src/main/resources/archetype-resources/__rootArtifactId__-test1/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ under the License.
3030
</parent>
3131
<artifactId>${rootArtifactId}-test1</artifactId>
3232

33-
</project>
33+
</project>

maven-archetype-plugin/src/it/projects/ARCHETYPE-241_filter-directory/archetype/src/main/resources/archetype-resources/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ under the License.
3333
<module>${projectName}-test2</module>
3434
</modules>
3535

36-
</project>
36+
</project>
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+
excludePatterns=build.log,invoker.properties,verify.groovy
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>org.apache.maven.plugins.archetype.its</groupId>
26+
<artifactId>archetype274-parent</artifactId>
27+
<version>1.0-SNAPSHOT</version>
28+
</parent>
29+
<artifactId>archetype274-archetype</artifactId>
30+
<packaging>maven-archetype</packaging>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-archetype-plugin</artifactId>
37+
<extensions>true</extensions>
38+
<configuration>
39+
<archetypeDirectory>src/main/resources</archetypeDirectory>
40+
</configuration>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
<archetype-descriptor name="include-condition-test">
21+
22+
<requiredProperties>
23+
<requiredProperty key="extraSupport"/>
24+
<requiredProperty key="anotherSupport"/>
25+
</requiredProperties>
26+
27+
<fileSets>
28+
<fileSet filtered="true" packaged="true">
29+
<directory>src/main/java</directory>
30+
<includes>
31+
<include>**/*.java</include>
32+
</includes>
33+
</fileSet>
34+
<fileSet filtered="false" packaged="false" includeCondition="${extraSupport}">
35+
<directory>src/main/resources</directory>
36+
<includes>
37+
<include>**/include-without-filter.txt</include>
38+
</includes>
39+
</fileSet>
40+
<fileSet filtered="true" packaged="false" includeCondition="${extraSupport}">
41+
<directory>src/main/resources</directory>
42+
<includes>
43+
<include>**/include-with-filter.txt</include>
44+
</includes>
45+
</fileSet>
46+
<fileSet filtered="true" packaged="false" includeCondition="${anotherSupport}">
47+
<directory>src/main/resources</directory>
48+
<includes>
49+
<include>**/exclude-with-filter.txt</include>
50+
</includes>
51+
</fileSet>
52+
<fileSet filtered="false" packaged="false" includeCondition="${anotherSupport}">
53+
<directory>src/main/resources</directory>
54+
<includes>
55+
<include>**/exclude-without-filter.txt</include>
56+
</includes>
57+
</fileSet>
58+
</fileSets>
59+
60+
</archetype-descriptor>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<groupId>${groupId}</groupId>
24+
<artifactId>${artifactId}</artifactId>
25+
<version>${version}</version>
26+
<packaging>pom</packaging>
27+
28+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
public class App {
21+
public static void main(String[] args) {
22+
System.out.println("This file should not be included");
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
this should be excluded: ${package}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
this should be excluded: ${package}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
this should be included: ${package}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
this should be included: ${package}

0 commit comments

Comments
 (0)