How most basic pom file get pluginManagement in Effective pom - maven

Just the most basic pom.xml file
Effective pom shows that it has pluginManagement with reference to maven-antrun-plugin
From where is it taken ?
How to avoid this ?
In bigger context such dependencies are shown as outdated by versions at "mvn clean versions:dependency-updates-report versions:plugin-updates-report", and they are outdated
$ mvn --version
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T00:58:13-07:00)
Maven home: /opt/maven/default
Java version: 1.8.0_152, vendor: Oracle Corporation
Java home: /opt/jdk/jdk1.8.0_152/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-693.2.2.el7.x86_64", arch: "amd64", family: "unix"
$ cat pom.xml
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my</groupId>
<artifactId>myproject</artifactId>
<version>0.0-SNAPSHOT</version>
<name>myname</name>
</project>
$ mvn help:effective-pom | grep -A 20 "<pluginManagement"
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
$ mvn versions:plugin-updates-report -DpluginUpdatesReportFormats=xml && cat target/plugin-updates-report.xml | grep -A 10 "<pluginManagements"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myname 0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.5:plugin-updates-report (default-cli) # myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.704 s
[INFO] Finished at: 2017-12-13T14:35:15-08:00
[INFO] Final Memory: 19M/304M
[INFO] ------------------------------------------------------------------------
<pluginManagements>
<pluginManagement>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<currentVersion>1.3</currentVersion>
<nextVersion>1.4</nextVersion>
<minors>
<minor>1.4</minor>
<minor>1.5</minor>
<minor>1.6</minor>
<minor>1.7</minor>

all such versions are from super pom
http://maven.apache.org/ref/3.5.2/maven-model-builder/super-pom.html , pluginManagement should be copied to pom.xml and version are upgraded in pom.xml to latest versions.

Related

Wrong version of maven-archetype-plugin running "mvn archetype:generate"

I created an archetype with maven, which includes archetype-post-generate.groovy, but it is not executed.
This is a fragment of the pom:
<properties>
<maven.archetype.version>3.2.0</maven.archetype.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>${maven.archetype.version}</version>
</plugin>
</plugins>
</pluginManagement>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>${maven.archetype.version}</version>
</extension>
</extensions>
</build>
However, the version of the plugin does not match the one defined in the pom:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources # standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources # standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) # standalone-pom ---
[INFO] Generating project in Interactive mode
Could that be the reason why it doesn't run the groovy script? Why does the version not match?
EDIT:
Complete POM with suggested changes:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>bp-archetype-bcore</artifactId>
<groupId>global.base</groupId>
<version>1.0.0</version>
<packaging>maven-archetype</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>${maven.archetype.version}</version>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>${maven.archetype.version}</version>
</extension>
</extensions>
</build>
</project>
Declaring a plugin just in <pluginManagement> adds nothing to the build. This is just kind of a template which configuration should be used if the plugin is declared in this or a child POM's <build><plugins> section.
See POM Reference, Plugin Management:
pluginManagement: [...] However, this only configures plugins that are actually referenced within the plugins element in the children or in the current POM. [...]

Does requireProperty not require a typical maven property in maven enforcer?

I am trying to require that a particular property is defined during the build using the maven enforcer plugin and the RequireProperty rule.
The relevant part of my pom.xml is this.
<?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>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<foo>bar</foo>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M1</version>
<executions>
<execution>
<id>require-properties</id>
<goals><goal>enforce</goal></goals>
<configuration>
<rules>
<requireProperty>
<property>${foo}</property>
<message>foo must be defined -${foo}-</message>
</requireProperty>
</rules>
<!-- Disabling, since it seems to not notice properties that are defined -->
<!-- <skip>true</skip> -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
When I run this, the enforcer rule fails.
$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------------< test:test >------------------------------
[INFO] Building test 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:3.0.0-M1:enforce (require-properties) # test ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireProperty failed with message:
foo must be defined -bar-
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.398 s
[INFO] Finished at: 2019-03-15T11:45:58-04:00
[INFO] ------------------------------------------------------------------------
It also fails if I define it on the command line.
$ mvn install -Dfoo=quz
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------------< test:test >------------------------------
[INFO] Building test 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:3.0.0-M1:enforce (require-properties) # test ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireProperty failed with message:
foo must be defined -quz-
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
Is this rule not for <properties> values? From the message, it is clear that the value is actually set.
I also tried adding <regex>.*</regex> with no effect.
How to I check that a maven property is set?
Real case details
In the example, the property is defined in the pom, so it sort of redundant to include the check. In the real case, the plugin is defined in the parent pom and the property is defined in the leaf pom. The behavior is the same in the real case.
mvn -version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T14:41:47-04:00)
Maven home: /usr/local/Cellar/maven/3.6.0/libexec
Java version: 10.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.3", arch: "x86_64", family: "mac"

Maven ignoring plugin version from pluginManagement in profile of submodule

I'm defining plugin versions in the <pluginManagement> section of a parent POM and want to use them in the <plugins> section of submodules.
This is working, unless the plugin is being used inside a profile of a submodule. In this case, the version from the parent POM's <pluginManagement> section is ignored.
Output of mvn -v:
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_102, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre
Default locale: de_DE, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac"
./pom.xml:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<prerequisites>
<maven>3.1.0</maven>
</prerequisites>
<modules>
<module>project1</module>
</modules>
<groupId>org.example.test</groupId>
<artifactId>test-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
./project1/pom.xml:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<prerequisites>
<maven>3.1.0</maven>
</prerequisites>
<parent>
<groupId>org.example.test</groupId>
<artifactId>test-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>project1</artifactId>
<profiles>
<profile>
<id>p1</id>
<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Output of mvn versions:display-plugin-updates:
$ mvn versions:display-plugin-updates
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] test-parent
[INFO] project1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test-parent 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:display-plugin-updates (default-cli) # test-parent ---
[INFO]
[INFO] All plugins with a version specified are using the latest versions.
[INFO]
[INFO] Project defines minimum Maven version as: 3.1.0
[INFO] Plugins require minimum Maven version of: 3.1.0
[INFO] Note: the super-pom from Maven 3.3.9 defines some of the plugin
[INFO] versions and may be influencing the plugins required minimum Maven
[INFO] version.
[INFO]
[INFO] No plugins require a newer version of Maven than specified by the pom.
[INFO]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building project1 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:display-plugin-updates (default-cli) # project1 ---
[INFO]
[INFO] All plugins with a version specified are using the latest versions.
[INFO]
[WARNING] The following plugins do not have their version specified:
[WARNING] com.github.eirslett:frontend-maven-plugin ................. 0.0.26
[INFO]
[INFO] Project defines minimum Maven version as: 3.1.0
[INFO] Plugins require minimum Maven version of: 3.1.0
[INFO] Note: the super-pom from Maven 3.3.9 defines some of the plugin
[INFO] versions and may be influencing the plugins required minimum Maven
[INFO] version.
[INFO]
[INFO] No plugins require a newer version of Maven than specified by the pom.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] test-parent ........................................ SUCCESS [ 0.851 s]
[INFO] project1 ........................................... SUCCESS [ 0.314 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.649 s
[INFO] Finished at: 2016-09-16T16:03:04+02:00
[INFO] Final Memory: 13M/247M
[INFO] ------------------------------------------------------------------------
I can duplicate the information from the <pluginManagement> section of the parent POM inside the submodules to make it work, but I want to avoid this for obvious reasons.
Maven is not ignoring it, you can check it by executing the following:
mvn -pl project1 help:effective-pom -Doutput=noProfilePom.xml
The effective-pom goal will:
Displays the effective POM as an XML for this build, with the active profiles factored in.
Checking the noProfilePom.xml generated, you will see what effectively Maven will run when building the pom.xml of the project1 module.
There we can see:
<pluginManagement>
<plugins>
...
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
...
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
Hence, the pluginManagement has been merged properly (taken from the parent), while the plugins section doesn't provide it.
But running the following:
mvn -pl project1 -Pp1 help:effective-pom -Doutput=withProfilePom.xml
Note: we are also activating the profile via -Pp1 as part of the goal execution.
As part of the generated withProfilePom.xml will have:
<pluginManagement>
<plugins>
...
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
This time the p1 profile was active and its has properly injected into the plugins section its plugin declaration, taking then its version from the pluginManagement of the parent.
Hence: the pluginManagement section is not ignored by plugins declared in a profile.

Why is PMD OK from command line, but does not work from within Maven?

After many years of successful use of PMD with Ant, I am now trying without success to get PMD to work from within Maven.
To illustrate my problem, I have created a simple Maven system (based upon the Maven tutorial "my-app" hello world program). It differs only in the inclusion of a line of code which should trigger a PMD error using the basic ruleset:
Boolean bar = new Boolean("true");
When I run PMD from the command line, the problem in the code is revealed:
run.sh pmd -d src/main/java -f text -R rulesets/java/basic.xml -language java
maven-pmd-example/src/main/java/com/mycompany/app/App.java:11 Avoid instantiating Boolean objects; reference Boolean.TRUE or Boolean.FALSE or call Boolean.valueOf() instead.
However, when I run pmd from within Maven, the problem in the code is not revealed:
-> mvn pmd:check
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-pmd-plugin:2.7.1:check (default-cli) # my-app >>>
[INFO]
[INFO] --- maven-pmd-plugin:2.7.1:pmd (pmd) # my-app ---
[WARNING] Unable to locate Source XRef to link to - DISABLED
[INFO]
[INFO] <<< maven-pmd-plugin:2.7.1:check (default-cli) # my-app <<<
[INFO]
[INFO] --- maven-pmd-plugin:2.7.1:check (default-cli) # my-app ---
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.375s
[INFO] Finished at: Sun Feb 03 15:38:02 HST 2013
[INFO] Final Memory: 12M/309M
[INFO] ------------------------------------------------------------------------
Here is the pom.xml:
<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>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.7.1</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<minimumPriority>0</minimumPriority>
<rulesets>
<ruleset>rulesets/basic.xml</ruleset>
</rulesets>
<targetJdk>1.6</targetJdk>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
</build>
</project>
I have uploaded this example system to GitHub so you can see the entire example system (and download/play with it if you would like):
https://github.com/philipmjohnson/maven-pmd-example
It's because you have set the minimumPriority to 0, which will effectively prevent PMD from evaluating any rules at all (refer to this SO question for a discussion on priority).
Please refer to this section of the goal documentation for the PMD plugin for more information.
I'd suggest modifying the configuration to set the minimumPriority to 2 so that you can fail the build for BooleanInstantiation.
Hope this helps!

How to generate jar for maven parent project

Maven requires a parent project to have
<packaging>pom</packaging>
clause in the parent's pom.xml. When such a project installed, only a pom-file generated into the maven repository. Jar-file is not generated, no matter if the parent project has any Java code. That forces me to have extra empty parent projects, which is overkill. Logically, some of my libraries could be parents at the same time.
Is there a way to generate both pom and jar files for a parent project without removing/adding the packaging clause between installs?
Use Maven Jar Plugin and Maven Build Helper. Example POM:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>test-${project.version}</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Maven build results:
mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- build-helper-maven-plugin:1.7:attach-artifact (attach-artifacts) # test ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default) # test ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) # test ---
[INFO] Installing /home/username/projects/test/pom.xml to /home/username/.m2/repository/test/test/1.0/test-1.0.pom
[INFO] Installing /home/username/projects/test/test-1.0 to /home/username/.m2/repository/test/test/1.0/test-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.805s
[INFO] Finished at: Thu Sep 06 13:33:20 EDT 2012
[INFO] Final Memory: 4M/119M
[INFO] ------------------------------------------------------------------------
A note on Maven practices:
Parent modules are typically where you define the dependencies and plugins used in common by all your child modules. It rarely has output of its own. You probably want to have a "distribution" sub-module that aggregates all your other module artifacts, rather than attempting to do it in the parent module.

Resources