How can I transform string property in Maven pom before using it value? - maven

I have artifactId of my Maven module. And I want use it for setting value of Main class in build plugin.
But I need transform value of artifactId, before using it.
For instance:
<artifactId>some-usefull-module</artifactId>
and then I use it somewhere like that:
<mainClass>${artifactId}</mainClass>
But I need to get from some-usefull-module the next value SomeUsefullModule. Can I do this in Maven pom.xml?

The build helper lets you set properties via regex:
https://www.mojohaus.org/build-helper-maven-plugin/regex-property-mojo.html

Related

Maven variable : Dollar vs Arobase?

In this documentation, it is suggested to use "#{argLine}". What is the difference between "#{argLine}" and "${argLine}" ? Is there a scope difference ?
Is this documented in maven doc ? (can't find it...).
#{argLine} instead of the usual ${argLine} is called "late property evaluation". This is only supported by the Maven Surefire Plugin.
From the documentation:
How do I use properties set by other plugins in argLine?
Maven does property replacement for ${...} values in pom.xml before any plugin is run. So Surefire would never see the place-holders in its argLine property.
Since the Version 2.17 using an alternate syntax for these properties, #{...} allows late replacement of properties when the plugin is executed, so properties that have been modified by other plugins will be picked up correctly.

Maven: how do I pass the artifactItems configuration to the dependency copy plugin from the command line?

I'd like the execute the Dependency Copy Plugin from the command line without the need to change the pom.xml file. I need to pass all configuration options from the command line. I can find some references to do it:
mvn -DuseRepositoryLayout=true dependency:copy
The problem is that I don't know how to set the <artifactItems><artifactItem> properties from the command line.
How would I invoke maven dependency copy plugin passing all necessary parameters in the command line?
You seem to mix two distinct goals :
copy-dependencies (referenced in your example) :
Goal that copies a list of artifacts from the repository to defined
locations.
copy (referenced in your link)
Goal that copies the project dependencies from the repository to a
defined location.
A user property in a mojo such as copy-dependencies provides a way to set a property from the command line with the -DMyUserProperty syntax.
From the copy plugin documentation you refer, you can read that the artifact property has as user property artifact.
So the example passing it from the command line is valid :
mvn dependency:copy -Dartifact=mygroupId:myartifactId:myversion
But the same plugin documentation doesn't specify any user property defined for the artifactItems property.
Besides, it is clearly stated :
Use artifactItems within the POM configuration.
So you are stuck to set artifactItems from a POM file.
As you don't want to bother with a POM and that you prefer to specify externally the dependencies to copy, dependency:copy-dependencies that provides a service enough close to which of copy-dependencies should better fit to your need as contrary to copy-dependencies, it provides a user property to include/exclude artifactIds/groupIds:
User Property: includeArtifactIds
...
User Property: includeGroupIds
You could so write something like :
mvn dependency:copy-dependencies -DincludeArtifactIds="myArtifactOne,
myArtifactTwo,..." -DincludeGroupIds="myGroupIdOne, myGroupIdTwo"

Using maven git describe plugin value as Jenkins build file name

I use the git-describe maven plugin to replace the POMs <version>${describe}</version> placeholder. mvn deploy needs a custom parameter passed in order for it to properly use the actual git describe value.
I'm now using Jenkins to build the project every time we push to the repo however it too doesn't properly use the actual git-describe value.
The jenkins build artifacts always end named project-${describe}
Are there any suggestions on ways I can customize the file names or force it to use the git-describe result? Otherwise I may be back to manual versioning...
The version property does not allow variable substitution. The first link I found googling this was this SO question.
You'll have to use one of the versioning maven plugins. The maven release plugin is the most popular, but you might find that the versions maven plugin better meets your requirements.

Creating Maven Archetype: is there a way to specify a default groupId

I have created a maven archetype using create-from-project. Is there a way to specify a default groupId?
This archetype is for internal use only, default groupId would save some keystrokes.
Thanks.
You can do this by specifying a propertyFile
This property file may contains few properties including default groupId. So something like this (in a file named something.properties):
groupId=com.company
and to create the archetype
mvn archetype:create-from-project -Darchetype.properties=../something.properties
another example here
If you want to use a "default" groupId because of company conventions, you can define it directly in the pom.xml in the archetype-resources directory (just replace the ${groupId} with your desired value. So it doesn't matter what the user of the archetype specifies.

setting maven properties by using shell command

I would like to obtain a value from a bash shell command and set a property using this value.
I am not sure setting the property is the way to go -- what I am trying to do is obtain a value from a shell command that is executed by Maven and to use this value to name the jar that is created.
More specifically, I would like to obtain using the git describe command the tag of a project and append this tag to the name of the jar.
I would like to do something like this within the pom:
tag = git describe
.
.
.
mv '$jarname'.jar '$jarname$tag'.jar
If I from the command line I execute
mvn jar:jar
I am not sure I can even affect the name of the resultant jar even if I had the git tag,
so that is another question.
I'm sure there are many other similar plugins out there, but you can use my exec-set-property plugin goal to set a Maven property using the output of a shell command.
What ended up working:
Goal was to be able to store SHA1 and git tag in the manifest of a jar.
Used the Maven plugin found here:
http://code54.com/blog/2012/04/30/buildversion-plugin.html
as well as the Maven buildnumber plugin.
These two plugins respectively set the Maven properties build-tag and buildNumber which
could then be specified in the Maven Jar plugin's manifestEntries section.

Resources