About maven plugin version tag - maven

We all know, when we use plugin or dependency in maven pom.xml, we must give the GAV(groupId, artifactId, version).That maven can know what plugin or dependency you want.
eg:
<!-- generate source plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<!-- ...others config -->
</plugin>
but, if i write like that:
eg:
<!-- generate source plugin -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<!-- ...others config -->
</plugin>
that is right!why?Maven says we must give GAV?Why that is right when i only give A?
So I want know if I don't give G and V,that maven will use what G or V?

The reason you don't have to specify the version is because it gets inherited from the Maven super POM (http://maven.apache.org/guides/introduction/introduction-to-the-pom.html). This super POM has a definition for the maven-source-plugin that specifies the version to be used.
If you declare other dependencies or plugins that are not declared in the super POM, then the version is required. Note however, that it is a good practise to explicitly declare the versions of the plugins you want to use in your own pom.xml to make your build more reproducible.
You can leave out the group id in this case, because maven will seach for org.apache.maven.plugins as groupId if no groupId was specified. So this will only work for the official Maven plugins that use this group id.

Related

Why maven uses difference plugin version to build different project

I have project A and B, use Maven 2.2.1
use it to build A, the result is positive, while user it to build B, it failed.
And I found that it use plugin with different version for the two project.
I don't know why ?? The pom.xml in both A and B does not specify the version of plugin.
I just want it will use the same version of plugin to build B project as A's.
Do you have any idea about it ? Many Thanks in advance.
The first issue is that you didn't defined the version of your plugins.
Usually you should have a kind of corporate/company pom which defines all used plugins with their particular version number. By using such approach you prevent exactly such situations.
The best you can do is to define the used plugins in your pom file via pluginManagement:
<project...>
.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-xyz-plugin</artifactId>
<version>yyyyy</version>
</plugin>
.
</plugins>
</pluginManagement>
.
</build>
.
</project>
If you do this you will get reproducible build results.
Furthermore you shouldn't use Maven 2.X anymore cause it's defined EoL.

read rivision number of pom file

I want to use svn revision number of pom file as its version.
In pom.xml, if we use buildnumber-maven-plugin, we need to define scm repository url, so that it can check to repository and use buildnumber.
But i want to ask that when we have checkedout code to our system, isn't there any way to get buildnumber without using scm urls. As revision number is stored as subversion in property of each file. we can see it if we right click on file and go to properties.
I want to use buildnumber in version tag of pom and other module's buildnumber in their vaersion tag in dependencies to them.
So if i can store all subversion numbers initially, infact earlier than resolving dependencies, then these subversions can be placed in version of module in dependency and also in version of each pom file.
Problem is that dependencies are resolved before plugin reads version number ( this is what i think), so it cannot resolve expression.
I tried it using properties-maven-plugin to write pom's version number in a file and then read it in other module's pom which is dependent on it. But dependencies are to be resolved before execution of any plugin is started.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<id>pre-clean-config</id>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${project.basedir}/../app.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
So, it is not working.
Starting with Maven 3.2.1 you can define a property ${revision} which you can use in your versions like this:
<project...>
<groupId>..</groupId>
<artifactId>...</artifactId>
<version>1.0-${revision}</version>
...
The result of this you need to give a property while calling Maven like this:
mvn -Drevision=123456 clean package
This will also work in dependencies etc. Currently one drawback is that you always creating a release from maven's point of view.

Using multiple JDK on the same machine for maven

I have to build java projects for different java versions. I'm using maven. I would like to specify all JDK locations in one configuration file (probably settings.xml) and then maven should choose correct one based on maven-compiler-plugin configuration specified in the pom and use compiler and standard library from this JDK to build project. Is it possible? I understand that I can use something like
JAVA_HOME=jdk6path mvn package
but it's not convenient.
Yes its possible. In settings.xml define properties like this:
<properties>
<java.home.1.4>C:\Program Files\Java\jdk1.4</java.home.1.4>
<java.home.5>C:\Program Files\Java\jdk1.5</java.home.5>
<java.home.6>C:\Program Files\Java\jdk1.6</java.home.6>
</properties>
Then in your POM file specify the properties
<compiler.source>1.6</compiler.source>
<compiler.target>1.6</compiler.target>
<compiler.compilerVersion>1.6</compiler.compilerVersion>
<compiler.jdk>${java.home.6}</compiler.jdk>
Then int he Maven compiler plugin pass in the executable:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compilerPluginVersion}</version>
<configuration>
<source>${compiler.source}</source>
<target>${compiler.target}</target>
<compilerVersion>${compiler.compilerVersion}</compilerVersion>
<executable>${compiler.executable}</executable>
</configuration>
</plugin>
You then control which is used by the properties. You can also set up different profiles in the POM with different property values to allow you to run the build with different versions without any change required to the pom.

mvn release plugin doesn't update included pom file

the maven release plugin doesn't update the pom files which are included:
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
....
....
<configuration>
<projectsDirectory>.</projectsDirectory>
<streamLogs>true</streamLogs>
<pomIncludes>
<pomInclude>pomDisContainer.xml</pomInclude>
</pomIncludes>
how can i force the release plugin to update all my pom.xml files?
regards
Perhaps you are looking at this from the wrong direction. Would a better solution be to have the Maven Invoker Plugin update the invoked pom.xml files every time instead?
The Invoker plugin will automatically replace properties in the pom, providing they are using the alternative syntax:
#project.version#
Will be replaced by the invoking project's version.

Update maven properties after a new release, using versions plugin

Im having a problem with a multimodule project in maven/jenkins.
For example my structure is like this:
---ProjectA
----pom.xml
--------ModuleA
---------pom.xml
--------ModuleB
---------pom.xml
---ProjectB
-----pom.xml
For example ModuleA has a dependency for something in ProjectB which is defined in ModuleA's pom except for the version which is only defined as property and is inherited from ProjectA's properties section.
I want to automate the release process to get rid of all the manual update of versions in all the poms. So after making a release of ProjectB I what to bump all references in ProjectA.
EDIT
More accurate I want to Release ProjectB which has to include a release of ProjectA (because of dependencies) and in the new Snapshots of ProjectA I want references to the newest ProjectB there is.
The maven plugin versions does this pretty well if one would specify the dependency and the version number in the same pom. My problem as you can see is that (I'm speculating) when version plugin tries to check the property field in ProjectA's pom the property can't be associated with a dependency. And I guess that versions plugin looks on the effective pom because it CAN find that the dependency in ModuleA's pom should be updated. It just can't update it due to the fact that its not defined there.
Would be much obliged for a solution which could keep my properties in the parent pom.
Thanks
Ok. SO I think I've worked something out, but i'll post it here for others to see.
So the thing I think is the problem is that the autoLinkItem only searches the current file for linkage and if you want a property to get assosiated with a dependency not specified in the same file one could explicitly tell the plugin in this.
Like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<properties>
<property>
<name>basis.version</name>
<dependencies>
<dependency>
<groupId>com.mycompany.app.basis</groupId>
<artifactId>ModuleBasis</artifactId>
</dependency>
</dependencies>
</property>
</properties>
<includeProperties>basis.version</includeProperties>
<generateBackupPoms>false</generateBackupPoms>
<allowSnapshots>true</allowSnapshots>
</configuration>
</plugin>

Resources