Override dependencies in a maven plugin (one plugin, different modules and different dependencies) - maven

I have a java project which has more modules. In some of the modules I use a plugin.
In that plugin I nee to override one from its dependencies. So far so good. Solved with adding of the desired dependency in the plugin definition.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xdoclet-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>xdoclet</groupId>
<artifactId>xjavadoc</artifactId>
<version>1.5-SNAPSHOT</version>
</dependency>
</dependencies>
...
But in one of the modules I need to override that dependency with some other version. I tried to solve that with the same way as overriding.
If I compile only that submodule alone, the correct dependency version is used, but if I compile whole project it does not work, because it uses the dependency from other modules for the plugin and not the one specified in the modules pom.
Any idea how to solve my problem?
Best regards, Filip

You can create property for your plugin version, e.g xdoclet.version and use it to override parent version.
<properites>
<xdoclet.version>1.5-SNAPSHOT</xdoclet.version>
</properties>
...
<plugin>enter code here
<groupId>org.codehaus.mojo</groupId>
<artifactId>xdoclet-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>xdoclet</groupId>
<artifactId>xjavadoc</artifactId>
<version>${xdoclet.version}</version>
</dependency>
</dependencies>
...

Related

is there any easiest way to modify dependency management version

i am in a situation to modify all the dependency management versions manually...
<dependencymanagement>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencymanagement>
i have more than 100 dependencies in the management which are not third party jars, all are project jars....
previously we have all the same versions,so we don't have any issues to modify....
now we introduced different versions for each module, for each and every time modifying the dependency management manually is some what difficulty...is there any easiest way to update by using the commands or any plugins.
You could use the Versions Maven Plugin which allows you to execute goals in order to handle versions, check the list here.
If you have 100-odd dependencies as you have mentioned, you should be using a repository manager like nexus or artifactory. Once you deploy your dependencies to the repository manager, then what #patric-lc suggests will work.

Update all versions in maven

I've got a maven project with a large number of sub-projects with many dependencies. Now I'd like to update all versions of my pom files to a new version and rebuild them. For example if I've got a a pom like that:
<parent>
<groupId>theparentId</groupId>
<artifactId>theParentArtifact</artifactId>
<version>2.0</version>
<relativePath>..</relativePath>
</parent>
<artifactId>eaics-wsa-model</artifactId>
<packaging>model</packaging>
<dependencies>
<dependency>
<groupId>groupId1</groupId>
<artifactId>artifactId1</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>groupId2</groupId>
<artifactId>artifactId2</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>groupId3</groupId>
<artifactId>artifactId3</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
I need to update the dependencies of groupId1 to groupId3 to a new version which doesn't exist jet. Because I also need to "create" a new updated version of my dependencies themself.
Because the dependencies in their pom.xml look like that at the moment:
<groupId>groupId3</groupId>
<artifactId>artifactId3</artifactId>
<version>1.2</version>
As you see, the version is on 1.2 but needs to be updated to 1.3 before the dependency uses it.
So is there a way to recursively update all pom (versions)? If it's possible in Java with MavenXpp3Reader etc. great. But is there a more simple method? Because my fear is, that I can't build my projects after that, because I think they don't build recursively and won't find the new dependency versions.
You can update all the pom's version using versions-maven-plugin There a some examples that can help you.

dependencies Needed by a mvn plugin are not resolved

I have created a Maven plugin called foo that needs a JAR file say xyz.jar in order to compile and run. In the pom file of plugin project foo I have provided xyz.jar with provided scope in the dependencies section. I do not want to package the xyz.jar file in the plugin.
The plugin foo will be triggered by another project say bar in its install phase. So I added the plugin foo in the <build><plugins><plugin>...</plugin></plugins></build> section of the project bar. Project bar has the dependency of xyz.jar in its <dependencies> section. When I run install goal the classes present in the xyz.jar and which are referred by the plugin foo throw ClassNotFoundException at runtime.
How can I resolve this?
Every plugin and the Maven Project have separated classloaders, that's why it is not working. In general a plugin shouldn't depend on a dependency of the project. So remove the provided scope will solve it. Manipulating the classloader is very tricky.
I do not want to package the xyz.jar file in the plugin.
Well, it is not. It is just a reference.
I can't tell what your plugin is doing. I suggest to have a look at the plugins from Apache Maven or Codehaus Mojo to see how it is done.
Project and plugin dependencies are separated in Maven. If your plugin requires some external jars you need to provide them in <dependencies> section located in the plugin tag (not together with your project dependencies). For example:
<build>
<plugins>
<plugin>
<groupId>...</groupId>
<artifactId>yourPluginArtifactId</artifactId>
<version>...</version>
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>xyz</artifactId>
<version>...</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

latest version of a dependency in maven archetype [duplicate]

Are there any preexisting Maven plugins or commands to update the dependencies in the POM?
Example: (if this was in my POM)
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
Is there a command or plugin I can run to get it to update the dependency to:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
Try the maven-versions-plugin, in particular, the versions:use-latest-versions goal.
I prefer using mvn versions:display-dependency-updates; this generates a report of which dependencies can be upgraded, but lets you modify the POMs yourself. There's also a display-plugin-updates command for plugins.
you can use dependencyManagement in your parent pom:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</dependencyManagement>
this way, you need to change the version only once in the parent POM
Personally, I think there should be an additional parameter in maven that would allow you to add to the pom.xml.
See post at http://maven.40175.n5.nabble.com/Is-there-any-maven-plugin-to-add-dependency-to-existing-pom-xml-td2839092.html#a5772853
Here, you can add the following to your pom.xml file:
<project>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.1</version>
</plugin>
...
</plugins>
...
</build>
...
</project>
...
Then backup your pom.xml file via version set command:
mvn versions:set -DnewVersion=9.9.9
Run latest versions:
mvn versions:use-latest-versions
and diff the pom.xml files, pom.xml and pom.xml.versionsBackup
No there is isn't. And be happy there is not. How would such a tool know how to upgrade your dependencies?
With breakages possibly happening between minor versions, it would be a disaster waiting to happen.
But you can always write your own Mojo for that.
get latest version of dependency from Maven repository
compare with version from pom.xml
rewrite pom.xml
run mvn test
?
Profit!
I had the same kind of problem and finally solved it by writing a bash script.
GitHub repository - Update POM Shell
This is a shell script that allows you to update a dependency on different modules directly from the command line.
It is particularly useful when you need to update one or more dependencies on different modules at once.

How to properly link two Maven projects?

I have two projects:
Project-Core
Project-Source
Project-Core POM.xml:
<groupId>com.company</groupId>
<artifactId>project-core</artifactId>
<packaging>jar</packaging>
<version>2.1</version>
Project-Source POM.xml:
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>project-core</artifactId>
<version>2.1</version>
<type>pom</type> (have tried leaving it out)
</dependency>
</dependencies>
I've done mvn clean install on Project-core, which installed the artifact in the local maven repository.
I am able to CD to Project-source and use mvn clean install (this installs Project-Source in the local maven repo as well), but I'm having trouble with NetBeans not finding the classes I need (from Project-Core) inside Project-Source.
What's a proper way of linking multiple projects? Since Project-Core produces a jar and that jar is installed in the local repository, it looks logical to only have to list that jar as a dependency on my Project-Source project. Is anything else needed?
You specified that the dependency "project-core" is of type "pom", but from the declaration it has packaging "jar" ?
Try:
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>project-core</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
Edit:
I've created a simple test project which worked just fine to use in Netbeand 7.0.1. Take a look and see if it gives you any hints.Code snippet

Resources