Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support addition of license header to generated files #437

Merged
merged 4 commits into from
Apr 30, 2024
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 @@ -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>
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
</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;
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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