From 2fb18772874530c96885920f800e5568b80f727e Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Tue, 4 Jun 2024 14:06:42 +0200 Subject: [PATCH 1/2] Add warning in strange case The least we can do is compare two language: one of known 'pom' type and other of project artifact: if both equal, and jar plugin is "aware" it creates JAR that is "java" language, emit a warning. --- src/it/MNG-8137/invoker.properties | 18 ++++++++ src/it/MNG-8137/pom.xml | 42 +++++++++++++++++++ src/it/MNG-8137/verify.groovy | 21 ++++++++++ .../maven/plugins/jar/AbstractJarMojo.java | 11 +++++ 4 files changed, 92 insertions(+) create mode 100644 src/it/MNG-8137/invoker.properties create mode 100644 src/it/MNG-8137/pom.xml create mode 100644 src/it/MNG-8137/verify.groovy diff --git a/src/it/MNG-8137/invoker.properties b/src/it/MNG-8137/invoker.properties new file mode 100644 index 00000000..b0222bb1 --- /dev/null +++ b/src/it/MNG-8137/invoker.properties @@ -0,0 +1,18 @@ +# 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 = jar:jar diff --git a/src/it/MNG-8137/pom.xml b/src/it/MNG-8137/pom.xml new file mode 100644 index 00000000..4663411a --- /dev/null +++ b/src/it/MNG-8137/pom.xml @@ -0,0 +1,42 @@ + + + + 4.0.0 + + org.apache.maven.plugins.jar.it + mng-8137 + pom + 1.0 + it-mng-8137 + + Project w/ pom packaging and jar:jar invoked + + + + + org.apache.maven.plugins + maven-jar-plugin + @project.version@ + + + + + diff --git a/src/it/MNG-8137/verify.groovy b/src/it/MNG-8137/verify.groovy new file mode 100644 index 00000000..09178219 --- /dev/null +++ b/src/it/MNG-8137/verify.groovy @@ -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. + */ + +String log = new File(basedir, 'build.log').text +assert log.contains('[WARNING] Language of project is \'none\', this is most probably not what you want') diff --git a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java index 9a2bc229..86d142c5 100644 --- a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java +++ b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java @@ -22,10 +22,12 @@ import java.nio.file.FileSystems; import java.util.Arrays; import java.util.Map; +import java.util.Objects; import java.util.Optional; import org.apache.maven.archiver.MavenArchiveConfiguration; import org.apache.maven.archiver.MavenArchiver; +import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -130,6 +132,9 @@ public abstract class AbstractJarMojo extends AbstractMojo { @Component private MavenProjectHelper projectHelper; + @Component(hint = "pom") + private ArtifactHandler pomArtifactHandler; + /** * Require the jar plugin to build a new JAR even if none of the contents appear to have changed. By default, this * plugin looks to see if the output jar exists and inputs have not changed. If these conditions are true, the @@ -351,6 +356,12 @@ public void execute() throws MojoExecutionException { throw new MojoExecutionException("You have to use a classifier " + "to attach supplemental artifacts to the project instead of replacing them."); } + if (Objects.equals( + pomArtifactHandler.getLanguage(), + getProject().getArtifact().getArtifactHandler().getLanguage())) { + getLog().warn("Language of project is '" + pomArtifactHandler.getLanguage() + + "', this is most probably not what you want"); + } getProject().getArtifact().setFile(jarFile); } } From e90b4b5f935acbca6180f1cb39e9a2982d56199a Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Tue, 4 Jun 2024 18:21:49 +0200 Subject: [PATCH 2/2] Use "common ground" for Maven3 and Maven4 --- src/it/MNG-8137/verify.groovy | 2 +- .../maven/plugins/jar/AbstractJarMojo.java | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/it/MNG-8137/verify.groovy b/src/it/MNG-8137/verify.groovy index 09178219..89fa8fcc 100644 --- a/src/it/MNG-8137/verify.groovy +++ b/src/it/MNG-8137/verify.groovy @@ -18,4 +18,4 @@ */ String log = new File(basedir, 'build.log').text -assert log.contains('[WARNING] Language of project is \'none\', this is most probably not what you want') +assert log.contains('[WARNING] The project packaging language is NOT \'java\'; this is most probably not what you want') diff --git a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java index 86d142c5..b8b6f7d6 100644 --- a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java +++ b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java @@ -22,12 +22,11 @@ import java.nio.file.FileSystems; import java.util.Arrays; import java.util.Map; -import java.util.Objects; import java.util.Optional; import org.apache.maven.archiver.MavenArchiveConfiguration; import org.apache.maven.archiver.MavenArchiver; -import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; @@ -132,8 +131,8 @@ public abstract class AbstractJarMojo extends AbstractMojo { @Component private MavenProjectHelper projectHelper; - @Component(hint = "pom") - private ArtifactHandler pomArtifactHandler; + @Component + private ArtifactHandlerManager artifactHandlerManager; /** * Require the jar plugin to build a new JAR even if none of the contents appear to have changed. By default, this @@ -356,11 +355,13 @@ public void execute() throws MojoExecutionException { throw new MojoExecutionException("You have to use a classifier " + "to attach supplemental artifacts to the project instead of replacing them."); } - if (Objects.equals( - pomArtifactHandler.getLanguage(), - getProject().getArtifact().getArtifactHandler().getLanguage())) { - getLog().warn("Language of project is '" + pomArtifactHandler.getLanguage() - + "', this is most probably not what you want"); + + if (!"java" + .equals(artifactHandlerManager + .getArtifactHandler(getProject().getPackaging()) + .getLanguage())) { + getLog().warn( + "The project packaging language is NOT 'java'; this is most probably not what you want"); } getProject().getArtifact().setFile(jarFile); }