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

Handle presence of index.jelly differently #345

Closed
wants to merge 13 commits into from
21 changes: 21 additions & 0 deletions src/it/missing-index-and-description/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#
# 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.
#

invoker.goals=-ntp clean package
invoker.buildResult=failure
19 changes: 19 additions & 0 deletions src/it/missing-index-and-description/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.40</version>
<relativePath />
</parent>
<groupId>org.jenkins-ci.tools.hpi.its</groupId>
<artifactId>missing-index-and-description</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>MyNewPlugin</name>
<properties>
<jenkins.version>2.249.1</jenkins.version>
<hpi-plugin.version>@project.version@</hpi-plugin.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package org.jenkinsci.tools.hpi.its;
public class X {}
22 changes: 22 additions & 0 deletions src/it/missing-index-and-description/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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.
*/

assert new File(basedir, 'build.log').getText('UTF-8').contains("Create src/main/resources/index.jelly:");

return true;
2 changes: 1 addition & 1 deletion src/it/missing-index/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<version>1.0-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>MyNewPlugin</name>
<description>Deprecated spot for plugin description.</description>
<description>Deprecated spot for plugin "description" with &lt;4 special chars.</description>
<properties>
<jenkins.version>2.249.1</jenkins.version>
<hpi-plugin.version>@project.version@</hpi-plugin.version>
Expand Down
2 changes: 1 addition & 1 deletion src/it/missing-index/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
* under the License.
*/

assert new File(basedir, 'build.log').getText('UTF-8').contains("create src/main/resources/index.jelly:");
assert new File(basedir, 'build.log').getText('UTF-8').contains("A default one will be created using the description of the pom.xml");
twasyl marked this conversation as resolved.
Show resolved Hide resolved

return true;
68 changes: 63 additions & 5 deletions src/main/java/org/jenkinsci/maven/plugins/hpi/HpiMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.Artifact;
Expand All @@ -30,6 +35,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
Expand Down Expand Up @@ -136,11 +142,24 @@ private void performPackaging()
jarArchiver.addConfiguredManifest(manifest);
File indexJelly = new File(getClassesDirectory(), "index.jelly");
if (!indexJelly.isFile()) {
throw new MojoFailureException("Missing " + indexJelly + ". Delete any <description> from pom.xml and create src/main/resources/index.jelly:\n" +
"<?jelly escape-by-default='true'?>\n" +
"<div>\n" +
" The description here…\n" +
"</div>");
final String projectDescription = getProjectDescription();
if (projectDescription != null) {
getLog().warn("src/main/resources/index.jelly does not exist. A default one will be created using the description of the pom.xml");
try (final FileOutputStream fos = new FileOutputStream(indexJelly);
final OutputStreamWriter indexJellyWriter = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
indexJellyWriter.write("<?jelly escape-by-default='true'?>\n" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be setting an XML encoding line.

<?xml version="1.0" encoding="UTF-8"?>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous error suggested:

throw new MojoFailureException("Missing " + indexJelly + ". Delete any <description> from pom.xml and create src/main/resources/index.jelly:\n" +
                    "<?jelly escape-by-default='true'?>\n" +
                    "<div>\n" +
                    "    The description here…\n" +
                    "</div>");

🤣

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it

"<div>\n" +
StringEscapeUtils.escapeXml(projectDescription) + "\n" +
"</div>");
indexJellyWriter.flush();
}
} else {
throw new MojoFailureException("Missing " + indexJelly + ". Create src/main/resources/index.jelly:\n" +
"<?jelly escape-by-default='true'?>\n" +
"<div>\n" +
" The description here…\n" +
"</div>");
}
}
jarArchiver.addDirectory(getClassesDirectory());
archiver.createArchive(session, project, archive);
Expand Down Expand Up @@ -174,4 +193,43 @@ private void performPackaging()

}

/**
* When calling {@code project.getDescription()}, it may return the description set in the parent project. What we
* want is the effective description defined in the project's pom.xml and not an inherited value.
*
* This method checks the project's description and if a value is found, compares it to the parent's one. In case
* they are the same, we consider this project has no description.
*
* @return The effective project's description or {@code null} if none or inherited from parent.
*/
private String getProjectDescription() {
String description = project.getDescription();
if (description != null) {
description = description.trim();
} else {
return null;
}

if (description.isEmpty()) {
return null;
}

final MavenProject parent = project.getParent();
if (parent == null) {
// Should not happen as a Jenkins plugin should have a parent.
getLog().debug("When getting the project description, it seems the project has no parent");
return description;
}

String parentDescription = parent.getDescription();
if (parentDescription != null) {
parentDescription = parentDescription.trim();
}

if (description.equals(parentDescription)) {
return null;
}

return description;
}
}