maven update pom property - maven

I'm looking for a way to update pom property to given value, i.e. my pom.xml contains:
<properties>
<abc.def>aaaaa</abc.def>
<properties>
now i want to call :
mvn some_plugin:some_goal -Dabc.def=XYZ
and finally my pom.xml should looks like
<properties>
<abc.def>XYZ</abc.def>
<properties>
I was reading about maven-release-plugin & versions-maven-plugin but i do not see there any matching goal.
Thank you in advance for any reply.

mvn versions:update-properties -Dproperties=[XYZ] -DincludeProperties={abc.def}
Read more here.
and here.
In short:
In versions-maven-plugin, the update-properties goal sets properties to the latest versions of specific artifacts.
includeProperties is a comma separated list of properties to update.
properties are any restrictions that apply to specific properties.

The accepted answer does not work for arbitrary values since it performs sanity checks (links to the documentation for set-property goal since for some reason the documentation for update-properties does not mention this).
To set some arbitrary value on a property use set-property since - as documented - it skips sanity checks:
mvn versions:set-property -Dproperty=your.property -DnewVersion=some_value

Ok, i found some case of solution. I'm using maven-replacer-plugin where:
my properties definition in pom.xml :
<properties>
<abc.def>aaaaa</abc.def>
<properties>
my plugin configuration :
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.2</version>
<configuration>
<file>pom.xml</file>
<replacements>
<replacement>
<token>${abc.def}</token>
<value>${replacer.abc.def}</value>
</replacement>
</replacements>
</configuration>
</plugin>
and finally my maven invocation :
mvn replacer:replace -Dreplacer.abc.def=XYZ
It works for me but I don know is there any better way to achieve it with maven-relase-plugin and/or versions-maven-plugin as #khmarbaise and #Conan said.

I agree with #khmarbaise above, the versions-maven-plugin will do just this, or you could move to the Maven Release Plugin if you want a much heftier approach to managing your versions, but you could also just run a script to sed the pom.xml file using Jenkins' BUILD_NUMBER environment variable, which is a quicker and dirtier approach.

Related

Maven calling plugin from command line in parent pom

My project sturcture:
- something-parent
-- something-one
-- something-two
something-parent -> pom.xml:
<packaging>pom</packaging>
<modules>
<module>something-one</module>
<module>something-two</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>io.github.something</groupId>
<artifactId>something-maven-plugin</artifactId>
<version>1.0.0</version>
</plugin>
</plugins>
</build>
Now I want to execute mvn something:help, but I can't: No plugin found for prefix 'something'.
If I remove modules section - it works. It also works in other modules.
It just doesn't work in parent module with modules section. I couldn't find any documentation or literally anything describing this, is this intended? Is there any workaround?
I know io.github.something:something-maven-plugin:1.0.0:help will work, but I need the shortcut version to work.
//Edit1 - I know about settings.xml solution, but it requires manual edit by user, I would like something on a project level
//Edit2 - found out another quirk, it works when I do mvn something:help -N
I understand that you want to use a short name to refer the plugin when calling it from the command line.
To do that, you have to define a plugin group in your settings.xml, as of https://maven.apache.org/settings.html#plugin-groups
In your case it would be:
<pluginGroups>
<pluginGroup>io.github.something</pluginGroup>
</pluginGroups>
This allows you to call:
mvn something:goal

Spring Boot Maven plugin: Cannot override arguments set in pom.xml

I am using the 2.7.4 version of the Spring Boot Maven plugin, and am puzzled about the behavior of arguments set in the pom.xml. Once there, it seems they cannot be overriden by specifying some on the command line.
According to the documentation
Arguments from the command line that should be passed to the application. Use spaces to separate multiple arguments and make sure to wrap multiple values between quotes. When specified, takes precedence over #arguments.
If I have this in my pom.xml:
<configuration>
<arguments>
<argument>--oh_hello=there</argument>
</arguments>
</configuration>
Then I cannot override this by for example using mvn spring-boot:run -Dspring-boot.run.arguments="--hello=world".
The arguments seen when the Spring applications starts are stuck at what is specified in pom.xml. I expect to be able to override this. Am I misunderstanding, or is this a bug?
Full example on GitHub.
As counter-intuitive as it may seem, it's conventional for a Maven plugin to behave in this way, with configuration in the pom.xml taking precedence over command-line configuration.
The reference documentation for Spring Boot's Maven plugin recommends using a project property to allow a setting to be configured on the command line. In your case, that would look something like this:
<properties>
<run.arguments>--hello=there</run.arguments>
</properties>
<configuration>
<arguments>
<argument>${run.arguments}</argument>
</arguments>
</configuration>
mvn spring-boot:run -Drun.arguments="--hello=world"

How can I specify complex Maven configuration options on the command line?

For example, I want to use the Wildfly deploy plugin, as outlined here:
http://docs.jboss.org/wildfly/plugins/maven/latest/deploy-artifact-mojo.html
To deploy, I would use a command like mvn wildfly:deploy -Dfilename=my.ear. But let's say I want to deploy to a particular server group. Using a POM, I would add:
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.1.0.Alpha1</version>
<configuration>
<domain>
<server-groups>
<server-group>main-server-group</server-group>
</server-groups>
</domain>
</configuration>
</plugin>
But if I can't change the POM, how would I pass this configuration in on the CLI or in $HOME\.m2\settings.xml?
The usage page indicates a configuration "type" of org.wildfly.plugin.deployment.domain.Domain for a "domain" option but I don't know how to type those options out on the CLI. Obvious answers like -Ddomain.server-groups.server-group=my-server-group don't seem to work.
If you could change the pom using a property like <server-group>${server.group}</server-group> should work. I don't think maven has support for complex attribute properties like that.
If that's not possible you could file a feature request.
Not exactly the answer to the overall question, but to your specific problem.
Changing the version in the pom to 1.2.2.Final, you can now do:
-Dwildfly.serverGroups=main-server-group
which I guess wasn’t available in 1.1.0.

log4j.properties in my /src/test/resources is not overriding main

How do I get my src/test/resources/log4j.properties file to override my /src/main/resources/log4j.properties during testing? The first ends up under target/test-classes, and the second is under target/classes.
Yes, I've looked for duplicates, but in this case, I don't have a mix of loggers - I'm using
only slf4j-log4j12.
You could use maven profiles combined with filtering and maven-war-plugin, all in pom.xml. With this you can define what you need to have in log4j.properties.
Example with logback:
Defining a variable in config.test.properties:
basepath_log=/var/log/ProjectName
Defining a placeholder in logback.xml (equivalent to log4j.properties):
<appender>
...
<file>${basepath_log}/ProjectName.log</file>
...
</appender>
Profile definition:
<profiles>
<profile>
<id>test</id>
<properties>
<targetenv>test</targetenv>
</properties>
</profile>
</profiles>
Filtering (pom.xml):
<filters>
<filter>src/main/resources/filter/config.${targetenv}.properties</filter>
</filters>
Then you could use profiles directly with maven using option -Ptest or selecting it in your IDE (Netbeans, Eclipse...)
Sorry, my bad. This bug suggested maybe my issue was a dependency on a bad version. Sure enough - 2.3 was too old; updated version behaves as expected.

read Maven variable from properties file using profile

I want to read a maven variable for configure a build plugin from a properties file. It's not needed in and further project files e.g. context files.
1) made a profile (it works, can use mvn ... -P private)
<profile>
<id>private</id>
<properties>
<env>private</env>
</properties>
</profile>
2) created the filter file with this content (it works)
foo.path=/home/foo/path
3) try to configure the plugin (does not work)
<build>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>foo-plugin</artifactId>
<version>${foo-plugin.version}</version>
<configuration>
<!--<fooPath>home/foo/path></fooPath> that works -->
<fooPath>${foo.path}</fooPath> <!--works not -->
</configuration>
...
</build>
Thx a lot
The name of your property is 'env' but you don't use env anywhere in your configuration.
When Maven docs mention "filter files" they usually mean a file used when processing resources (i.e. copying resources from /src/main/resources to target/classes). As far as I know the properties in those files aren't used for plugin configuration out-of-the-box. I have used the Codehaus properties-maven-plugin:read-project-properties goal do do what you are attempting. Make sure you bind the goal to the lifecycle before any plugins that need the properties for config.
Also, see this answer; you may load properties used to configure other plugins, but not to configure core Maven project elements.

Resources