Skip to content

Commit c90d219

Browse files
authored
Support addition of license header to generated files (#437)
Add ability to set license header (verbatim text of file) and push that into generated files.
1 parent 676f104 commit c90d219

File tree

6 files changed

+102
-6
lines changed

6 files changed

+102
-6
lines changed

modello-core/src/main/java/org/codehaus/modello/ModelloParameterConstants.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,12 @@ public class ModelloParameterConstants {
8080
*/
8181
public static final String XSD_ENFORCE_MANDATORY_ELEMENTS = "modello.xsd.enforce.mandatory.element";
8282

83+
/**
84+
* The license text as string, to be added to generated files, if needed.
85+
*
86+
* @since 2.3.1
87+
*/
88+
public static final String LICENSE_TEXT = "modello.license.text";
89+
8390
private ModelloParameterConstants() {}
8491
}

modello-core/src/main/java/org/codehaus/modello/plugin/AbstractModelloGenerator.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public abstract class AbstractModelloGenerator implements ModelloGenerator {
6666

6767
private String encoding;
6868

69+
private String licenseText;
70+
6971
@Inject
7072
private BuildContext buildContext;
7173

@@ -82,9 +84,12 @@ protected void initialize(Model model, Properties parameters) throws ModelloExce
8284

8385
generatedVersion = new Version(version);
8486

85-
packageWithVersion = Boolean.valueOf(getParameter(parameters, ModelloParameterConstants.PACKAGE_WITH_VERSION));
87+
packageWithVersion =
88+
Boolean.parseBoolean(getParameter(parameters, ModelloParameterConstants.PACKAGE_WITH_VERSION));
8689

8790
encoding = parameters.getProperty(ModelloParameterConstants.ENCODING);
91+
92+
licenseText = parameters.getProperty(ModelloParameterConstants.LICENSE_TEXT);
8893
}
8994

9095
protected Model getModel() {
@@ -108,13 +113,27 @@ protected String getEncoding() {
108113
}
109114

110115
protected String getHeader() {
116+
String header = getLicenseHeader();
117+
if (header == null) {
118+
return getGeneratedHeader();
119+
}
120+
header += System.lineSeparator();
121+
header += getGeneratedHeader();
122+
return header;
123+
}
124+
125+
protected String getGeneratedHeader() {
111126
String version = getClass().getPackage().getImplementationVersion();
112-
return "=================== DO NOT EDIT THIS FILE ====================\n"
113-
+ "Generated by Modello" + ((version == null) ? "" : (' ' + version)) + ",\n"
114-
+ "any modifications will be overwritten.\n"
127+
return "=================== DO NOT EDIT THIS FILE ====================" + System.lineSeparator()
128+
+ "Generated by Modello" + ((version == null) ? "" : (' ' + version)) + "," + System.lineSeparator()
129+
+ "any modifications will be overwritten." + System.lineSeparator()
115130
+ "==============================================================";
116131
}
117132

133+
protected String getLicenseHeader() {
134+
return licenseText;
135+
}
136+
118137
protected boolean isClassInModel(String fieldType, Model model) {
119138
try {
120139
return model.getClass(fieldType, generatedVersion) != null;

modello-maven-plugin/src/it/maven-model/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
<models>
2929
<model>src/main/mdo/maven.mdo</model>
3030
</models>
31+
<licenseText>The license of this file</licenseText>
3132
</configuration>
3233
<executions>
3334
<execution>
3435
<goals>
3536
<goal>java</goal>
3637
<goal>xpp3-reader</goal>
38+
<goal>xsd</goal>
3739
</goals>
3840
</execution>
3941
</executions>

modello-maven-plugin/src/it/maven-model/src/main/mdo/maven.mdo

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
| definition of these types
2424
|
2525
-->
26-
<model>
26+
<model xmlns="http://codehaus-plexus.github.io/MODELLO/2.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27+
xsi:schemaLocation="http://codehaus-plexus.github.io/MODELLO/2.3.0 https://codehaus-plexus.github.io/modello/xsd/modello-2.3.0.xsd"
28+
xml.namespace="http://maven.apache.org/POM/${version}"
29+
xml.schemaLocation="https://maven.apache.org/xsd/maven-${version}.xsd">
2730
<id>maven</id>
2831
<name>Maven</name>
2932
<description><![CDATA[
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
File generatedSources = new File(basedir, "target/generated-sources/modello")
21+
File generatedSite = new File(basedir, "target/generated-site/resources/xsd")
22+
assert generatedSources.exists()
23+
assert generatedSources.isDirectory()
24+
assert generatedSite.exists()
25+
assert generatedSite.isDirectory()
26+
27+
String javaSource = new File(generatedSources, "org/apache/maven/model/Model.java").text
28+
String xsdSource = new File(generatedSite, "maven-4.0.0.xsd").text
29+
30+
// due formatting issues (empty lines lost) let's stick with trivial license and assertion for now
31+
assert javaSource.contains("The license of this file")
32+
assert xsdSource.contains("The license of this file")

modello-maven-plugin/src/main/java/org/codehaus/modello/maven/AbstractModelloGeneratorMojo.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.File;
2626
import java.io.FileNotFoundException;
2727
import java.io.IOException;
28+
import java.nio.file.Files;
2829
import java.util.ArrayList;
2930
import java.util.Collections;
3031
import java.util.List;
@@ -95,6 +96,22 @@ public abstract class AbstractModelloGeneratorMojo extends AbstractMojo {
9596
@Parameter
9697
private List<String> packagedVersions = new ArrayList<String>();
9798

99+
/**
100+
* The contents of license header text, verbatim.
101+
*
102+
* @since 2.3.1
103+
*/
104+
@Parameter
105+
private String licenseText;
106+
107+
/**
108+
* The file that contains license header text. If both configured, the {@link #licenseText} prevails.
109+
*
110+
* @since 2.3.1
111+
*/
112+
@Parameter
113+
private File licenseFile;
114+
98115
/**
99116
* @since 1.0.1
100117
*/
@@ -160,11 +177,27 @@ public void execute() throws MojoExecutionException {
160177

161178
parameters.setProperty(ModelloParameterConstants.PACKAGE_WITH_VERSION, Boolean.toString(packageWithVersion));
162179

163-
if (packagedVersions.size() > 0) {
180+
if (!packagedVersions.isEmpty()) {
164181
parameters.setProperty(
165182
ModelloParameterConstants.ALL_VERSIONS, StringUtils.join(packagedVersions.iterator(), ","));
166183
}
167184

185+
if (licenseText != null || licenseFile != null) {
186+
String license = "";
187+
if (licenseText != null) {
188+
// licenseText prevails
189+
license = licenseText;
190+
} else {
191+
try {
192+
// load it up and hard fail if cannot, as it is user misconfiguration
193+
license = String.join(System.lineSeparator(), Files.readAllLines(licenseFile.toPath()));
194+
} catch (IOException e) {
195+
throw new MojoExecutionException("Could not load up license text from " + licenseFile, e);
196+
}
197+
}
198+
parameters.setProperty(ModelloParameterConstants.LICENSE_TEXT, license);
199+
}
200+
168201
customizeParameters(parameters);
169202

170203
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)