Skip to content

Commit

Permalink
Support addition of license header to generated files (#437)
Browse files Browse the repository at this point in the history
Add ability to set license header (verbatim text of file) and push that into generated files.
  • Loading branch information
cstamas committed Apr 30, 2024
1 parent 676f104 commit c90d219
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,12 @@ public class ModelloParameterConstants {
*/
public static final String XSD_ENFORCE_MANDATORY_ELEMENTS = "modello.xsd.enforce.mandatory.element";

/**
* The license text as string, to be added to generated files, if needed.
*
* @since 2.3.1
*/
public static final String LICENSE_TEXT = "modello.license.text";

private ModelloParameterConstants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public abstract class AbstractModelloGenerator implements ModelloGenerator {

private String encoding;

private String licenseText;

@Inject
private BuildContext buildContext;

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

generatedVersion = new Version(version);

packageWithVersion = Boolean.valueOf(getParameter(parameters, ModelloParameterConstants.PACKAGE_WITH_VERSION));
packageWithVersion =
Boolean.parseBoolean(getParameter(parameters, ModelloParameterConstants.PACKAGE_WITH_VERSION));

encoding = parameters.getProperty(ModelloParameterConstants.ENCODING);

licenseText = parameters.getProperty(ModelloParameterConstants.LICENSE_TEXT);
}

protected Model getModel() {
Expand All @@ -108,13 +113,27 @@ protected String getEncoding() {
}

protected String getHeader() {
String header = getLicenseHeader();
if (header == null) {
return getGeneratedHeader();
}
header += System.lineSeparator();
header += getGeneratedHeader();
return header;
}

protected String getGeneratedHeader() {
String version = getClass().getPackage().getImplementationVersion();
return "=================== DO NOT EDIT THIS FILE ====================\n"
+ "Generated by Modello" + ((version == null) ? "" : (' ' + version)) + ",\n"
+ "any modifications will be overwritten.\n"
return "=================== DO NOT EDIT THIS FILE ====================" + System.lineSeparator()
+ "Generated by Modello" + ((version == null) ? "" : (' ' + version)) + "," + System.lineSeparator()
+ "any modifications will be overwritten." + System.lineSeparator()
+ "==============================================================";
}

protected String getLicenseHeader() {
return licenseText;
}

protected boolean isClassInModel(String fieldType, Model model) {
try {
return model.getClass(fieldType, generatedVersion) != null;
Expand Down
2 changes: 2 additions & 0 deletions modello-maven-plugin/src/it/maven-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
<models>
<model>src/main/mdo/maven.mdo</model>
</models>
<licenseText>The license of this file</licenseText>
</configuration>
<executions>
<execution>
<goals>
<goal>java</goal>
<goal>xpp3-reader</goal>
<goal>xsd</goal>
</goals>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
| definition of these types
|
-->
<model>
<model xmlns="http://codehaus-plexus.github.io/MODELLO/2.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://codehaus-plexus.github.io/MODELLO/2.3.0 https://codehaus-plexus.github.io/modello/xsd/modello-2.3.0.xsd"
xml.namespace="http://maven.apache.org/POM/${version}"
xml.schemaLocation="https://maven.apache.org/xsd/maven-${version}.xsd">
<id>maven</id>
<name>Maven</name>
<description><![CDATA[
Expand Down
32 changes: 32 additions & 0 deletions modello-maven-plugin/src/it/maven-model/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

File generatedSources = new File(basedir, "target/generated-sources/modello")
File generatedSite = new File(basedir, "target/generated-site/resources/xsd")
assert generatedSources.exists()
assert generatedSources.isDirectory()
assert generatedSite.exists()
assert generatedSite.isDirectory()

String javaSource = new File(generatedSources, "org/apache/maven/model/Model.java").text
String xsdSource = new File(generatedSite, "maven-4.0.0.xsd").text

// due formatting issues (empty lines lost) let's stick with trivial license and assertion for now
assert javaSource.contains("The license of this file")
assert xsdSource.contains("The license of this file")
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -95,6 +96,22 @@ public abstract class AbstractModelloGeneratorMojo extends AbstractMojo {
@Parameter
private List<String> packagedVersions = new ArrayList<String>();

/**
* The contents of license header text, verbatim.
*
* @since 2.3.1
*/
@Parameter
private String licenseText;

/**
* The file that contains license header text. If both configured, the {@link #licenseText} prevails.
*
* @since 2.3.1
*/
@Parameter
private File licenseFile;

/**
* @since 1.0.1
*/
Expand Down Expand Up @@ -160,11 +177,27 @@ public void execute() throws MojoExecutionException {

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

if (packagedVersions.size() > 0) {
if (!packagedVersions.isEmpty()) {
parameters.setProperty(
ModelloParameterConstants.ALL_VERSIONS, StringUtils.join(packagedVersions.iterator(), ","));
}

if (licenseText != null || licenseFile != null) {
String license = "";
if (licenseText != null) {
// licenseText prevails
license = licenseText;
} else {
try {
// load it up and hard fail if cannot, as it is user misconfiguration
license = String.join(System.lineSeparator(), Files.readAllLines(licenseFile.toPath()));
} catch (IOException e) {
throw new MojoExecutionException("Could not load up license text from " + licenseFile, e);
}
}
parameters.setProperty(ModelloParameterConstants.LICENSE_TEXT, license);
}

customizeParameters(parameters);

// ----------------------------------------------------------------------
Expand Down

0 comments on commit c90d219

Please sign in to comment.