Monday, June 6, 2011

Maven build failure due to unresolved custom variables

The traditional maven build runs as: "mvn clean install". However, With a multiple module project structure, there is caveat to this approach.

Problem: The maven local respository contains the parent and child artifacts with their corresponding POM files. If the parent project used a custom variable say "release.number" which gets used in the child project, then "mvn install" would flush the POM file "as is" into the local repository, along with the built artifact for both parent and child. Now, down the line if any new project adds a dependency to the child project's artifact, then at the build time of the new project, the "release.number" referenced in the child dependency (that was supposed to be initialized in the parent POM file) remains unresolved.

Solution: This is because the POM file residing in the maven repository is only meant to dictate the group id, artifact id, version number etc, of the dependency and nothing more that that. So, the variables' value which is dynamic in nature will remain unresolved, as it is not participating in component build anymore. The solution (that I can think of for now) to this kind of problem is, to flush the artificat into the local repository in offline and not during the artificat build time. This can be done using the below mvn command:

mvn install:install-file -Dfile=your-artifact-1.0.jar \
[-DpomFile=your-pom.xml] \
[-Dsources=src.jar] \
[-Djavadoc=apidocs.jar] \
[-DgroupId=org.some.group] \
[-DartifactId=your-artifact] \
[-Dversion=1.0] \
[-Dpackaging=jar] \
[-Dclassifier=sources] \
[-DgeneratePom=true] \
[-DcreateChecksum=true]

Courtesy: http://maven.apache.org/plugins/maven-install-plugin/usage.html

This command doesn't take any mandatory pom.xml. Using -DpomFile parameter is optional, so please ignore it to addresss this kind of a scenario. Rather, use -DgeneratePom=true parameter which creates a POM file automatically, based on the other supplied parameters to the command.

No comments:

Post a Comment