Where does the configuration block of an execution actually go in pom.xml? - maven

It seems a very simple question, but something is not working right.
So given a pom.xml file I wanted to use the properties-maven-plugin. If I copy what is in the docs, meaning this code:
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>
${project.baseDir}/app.properties
</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And then I run using mvn properties:write-project-properties, I get an error:
Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0.0:write-project-properties (default-cli) on project backend: The parameters 'outputFile' for goal org.codehaus.mojo:properties-maven-plugin:1.0.0:write-project-properties are missing or invalid
BUT
when I edit the code above and move the configuration right next to executions and then run the command, everything works (even though IntelliJ underlines the configuration expression with red saying properties 'child' tag should be defined.
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
</execution>
</executions>
<configuration>
<outputFile>
${project.baseDir}/app.properties
</outputFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
I just randomly stumbled upon this answer here that solved this issue.
Why is this happening and where are the documentations for maven that would prevent me from spending couple hours wasting on a configuration file next time I need something?

The reason is as follows:
The first POM defines an execution for the Maven lifecycle together with its configuration. So it is meant to run when you call something like mvn clean install. With the configuration inside the execution, you just configured that execution (not any other execution, like the one you started from command line).
The second POM has a global configuration for the plugin goal. The execution in the POM does not matter (you can just delete it), but your command line execution will pick up this global configuration.
Does this make the behaviour more understandable?

Related

Build not finishing due to maven exec plugin

I have this multi module maven project, with one parent pom and many child poms.
when running maven install, my last module uses a maven exec plugin and runs a main method of some class. The thing is, it seems like the build is stopped once the main method finishes running.
This is my setup:
from my parent pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<skip>true</skip>
<executable>java</executable>
</configuration>
</plugin>
from my child pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.example.MainClass</mainClass>
<async>true</async>
<keepAlive>true</keepAlive>
</configuration>
</execution>
</executions>
</plugin>
I tried incorporating
<async>true</async>
<keepAlive>true</keepAlive>
but it still stops after the main method is executed. I do not see BUILD SUCCESS in the end...
The solution was to use exec:exec and not exec:java, because exec:java and maven build run on the same process while exec:exec runs on a different process.

Run Maven goal only in parent POM by activation

I am working on integrating a plugin into a multi-module project.
I am using a 3rd party plugin that essentially needs to only by run from the parent project (based on my understanding and usage of it). I tried to accomplish this by using a profile, like so:
<profiles>
<profile>
<id>run-my-guy</id>
<build>
<plugins>
<plugin>
<groupId>com.myproject</groupId>
<artifactId>myproject-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>runThing</goal>
</goals>
<inherited>false</inherited>
</execution>
</executions>
<inherited>false</inherited>
</plugin>
</plugins>
</build>
</profile>
</profiles>
I have several <inherited>false</inherited>, but if I run mvn help:all-profiles I can still see this profile in every single module. If I run my mvn package -P run-my-guy I see this get executed in every single subproject. I want the ability to activate this and I do not want it to be on by default.
If I try to add it the <build> section, like this:
<build>
<plugins>
<plugin>
<groupId>com.myproject</groupId>
<artifactId>myproject-maven-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>runThing</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here, I also have a few <inherited>false</inherited>, just to try and enforce that the plugin and the execution are not inherited. However, whenI run the package phase, or anything that includes that phase, the runThing goal is included.
How do I run a goal only by activation (like profile or some other feature, or just by explicitly running the goal) and only in the parent?
As shown in an answer for "Run a single Maven plugin execution?", it is now possible (since Maven 3.3.1) to specify an execution Id for a direct goal invocation.
pom.xml
<build>
<plugins>
<plugin>
<groupId>com.myproject</groupId>
<artifactId>myproject-maven-plugin</artifactId>
<inherited>false</inherited>
<executions>
<id>myproject-exec-id</id> <!-- note the execution Id -->
<execution>
<phase>none</phase>
<goals>
<goal>runThing</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And then invoking the goal from the command line uses the optional #executionId parameter:
mvn myproject:runThing#myproject-exec-id

Redirecting the nexus-staging-maven-plugin to an internal repository

I have a webjars project that I'd like to build snapshots and have them deployed locally (within our infrastructure) but have the releases be managed by webjars.org.
https://github.com/webjars/oscar/blob/15.0.0-RC1/pom.xml
The problem I'm facing is that they need the nexus-staging-maven-plugin defined as part of their release process. So I'd like to keep the POM tailored for them as much as possible, and just make our own deviations via command line (or worst case, profile) if possible.
Typically, if doing this from scratch, you'd probably introduce the staging plugin in a profile, but I don't think I have that option. (I might have to make the modification though and discuss it with them.)
I've never used the staging plugin before, so this is my first exposure and it isn't a pleasant one given what I'm trying to do. I feel like I'm fighting the system.
I thought I had something by specifying:
-DsonatypeOssDistMgmtSnapshotsUrl=http://myurl
and that pushes the artifact at the correct location, but then I can't figure out how to supply credentials. (401 unauthorized) I thought specifying the serverId might work, but no.
-DserverId=public-snapshots
http://books.sonatype.com/nexus-book/reference/staging-deployment.html
I then tried creating a profile, where I'd do something like this:
<profile>
<id>disable-staging</id>
<build>
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.5</version>
<configuration combine.self="override">
<executions>
<execution>
<id>injected-nexus-deploy</id>
<phase>none</phase>
</execution>
</executions>
</configuration>
</plugin>
</plugins>
</build>
</profile>
but that doesn't help. The effective-pom shows this:
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.5</version>
<extensions>true</extensions>
<executions>
<execution>
<id>injected-nexus-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration combine.self="override">
<executions>
<execution>
<id>injected-nexus-deploy</id>
<phase>none</phase>
</execution>
</executions>
</configuration>
</execution>
</executions>
<configuration combine.self="override">
<executions>
<execution>
<id>injected-nexus-deploy</id>
<phase>none</phase>
</execution>
</executions>
</configuration>
</plugin>
and I can't unbind the deploy phase.
I'm wondering if anyone has any ideas on what to do in this type of scenario?
You can use the Nexus Staging Plugin's property skipNexusStagingDeployMojo to achieve this:
mvn clean package deploy:deploy -DskipNexusStagingDeployMojo=true
-DaltDeploymentRepository=snapshots::default::https://my.nexus.host/content/repositories/snapshots-local
It is important to explicitly invoke package and then deploy:deploy, as otherwise (when only invoking mvn deploy) the default execution of the maven-deploy-plugin is suppressed by the Nexus Staging Plugin (even with the skip set to true).

run jasmine.js test as part of Maven 'test' goal

I have the following jasmine tests configuration in my project pom.xml:
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>test</goal>
<goal>jasmine</goal>
</goals>
</execution>
</executions>
<configuration>
<jsSrcDir>${basedir}/src/main/js</jsSrcDir>
<sourceIncludes>
<include>myCode.js</include>
</sourceIncludes>
<jsTestSrcDir>${basedir}src/test/js/specs</jsTestSrcDir>
<specIncludes>
<include>mySpec.js</include>
</specIncludes>
</configuration>
</plugin>
...
with this configuraton I can run jsamine:test and the test wil run.
I want to run the tests as part of the goal test, but Maven won't run them.
I even tried removing the line <goal>jasmine</goal> but Maven still won't run the jasmine tests.
What am I doing wrong?
You must also add the execution of the plugin like this into you project:
<project>
<build>
<plugins>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
cause the one you've defined in pluginManagement does not really execute the plugin.

Can I modify the Maven deploy phase to replace the maven-deploy-plugin with my own plugin?

I'm pretty new to Maven...
What I'm trying to do is skip the maven-deploy-plugin during the deploy phase, while replacing it with my own plugin (i.e. I'm deploying to a non-repository location).
I realize I could do this in multiple other ways, but the boss wants to be able to run:
mvn deploy
To get the results of my current workaround, which is disabling the maven-deploy-plugin (which seems to be disabling the entire deploy phase), and manually specifying the custom upload goal from the command line.
I'm currently failing to succeed in my mission with:
<executions>
<execution>
<phase>deploy</phase>
</execution>
</executions>
in the build/plugins/plugin section containing my plugin specification, since the deploy phase is skipped by:
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
Thanks!
disabling the maven-deploy-plugin (which seems to be disabling the entire deploy phase)
This is not correct. Disabling maven-deploy-plugin doesn't disable the entire deploy phase. This is how it should be done (looks like you're doing it already):
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Try this (untested) alternative for disabling the standard deploy plugin:
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
I want to build on #yegor256's answer a bit... 8 years, 4 months later!
I found myself here getting into the weeds on some legacy Maven configurations that were full of cruft. Coming from a Maven mindset, albeit some years between now and active hacking, I was re-familiarizing myself with the Maven lifecycle.
TLDR... mvn help:effective-pom is your friend. Use your IDE's tools for viewing the effective POM often (NetBeans makes it easy. I added a keyboard shortcut in IntelliJ.)
In the configuration I was reviewing, the previous developers had created two (2) deploy-file executions, one war, one jar.
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>deploy-war</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
... omitted ...
</configuration>
</execution>
<execution>
<id>deploy-jar</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
... omitted ...
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build>
I was aware that these executions would be appended to the default-deploy bound to the deploy phase and observed this behavior in the logs. The default-deploy would run, uploading an empty war file, then the deploy-war would run, uploading, and overwriting, the first war file.
Several options exist.
skip and combine.self="override" (my preference)
As presented, using <skip> as a <configuration> option is viable. It is safe and more portable than setting setting the <phase> to none.
However, it will be inherited by the other executions (certainly as presented). To prevent this, you must explicitly tell your additional <execution> configurations to not inherit.
...
...
<executions>
<execution>
<id>deploy-war</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration combine.self="override">
... omitted ...
</configuration>
</execution>
...
...
Override default-deploy
Another option, possibly more verbose and lest esoteric than combine.self="override" is to override the execution of the default-deploy <id> of the plugin.
...
<execution>
<id>default-deploy</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
...
This will not be inherited by the additional <executions>.
Another option
As #yegor256 notes, but in the additional configurations explicitly state <skip>false</skip> to "reset" the inherited <skip> from the plugin.
HTH.

Resources