Maven build parameter to exclude a pom dependency - maven

I have a project which has some dependency:
<dependency>
<groupId>mygroup</groupId>
<artifactId>myartifact</artifactId>
<version>1.0</version>
</dependency>
I would like to have the possibility to include/exclude the dependency optionally in build time, and would be nice to have some parameter in my build, for instance mvn clean package -mygroup or something similar.
I have search in different mvn plugins configuration settings, but I didn´t find it.
Some ideas/inputs?
Thanks

Related

Maven POM packaging with dependencies

I'm trying to construct a codebase where subsystems can be developed as maven modules, without the importing POM needing to concern itself with the internal structure of the maven module.
The "importing" pom
<project>
<artifactId>application</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<artifactId>submodule-1</artifactId>
</dependency>
</dependencies>
</project>
The "imported" pom
<project>
<artifactId>submodule-1</artifactId>
<packaging>pom</packaging>
<modules>
<module>api</module>
<module>implementation</module>
</modules>
<dependencies>
<dependency>
<artifactId>api</artifactId>
</dependency>
<dependency>
<artifactId>implementation</artifactId>
</dependency>
</dependencies>
</project>
This does seem to work, at least partially; the generated JARs appear to be on the classpath during mvn package. IntelliJ shows the application has a dependency on submodule-1 and transitively on api and implementation. However, mvn dependency:tree fails while building submodule-1 saying
Could not resolve dependencies for project submodule-1:pom:1.0.0-SNAPSHOT: Could not find artifact api:jar:1.0.0-SNAPSHOT
I'm trying to determine if this is a valid pattern, that of having packaging of pom, with defined dependencies which are also defined modules in the POM.
Have I stumbled upon a working-but-not-supported edge case, or is the dependency plugin broken in some way, or I'm breaking it in some way, or something else?

Add a dependency to a maven module without sources

I have a multi-module Maven project, and some of the modules use Ant for the build.
One such module should depend on another, but the target module doesn't have any Java sources and so it doesn't produce any artifact. Can I add a dependency on such a module, or do I have to produce an empty JAR?
If the only goal is to fix the build order, try adding a dependency on the POM.
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>project-built-by-ant</artifactId>
<version>1.0</version>
<type>pom</type>
</dependency>

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

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>
...

Maven cannot resolve a jar which I can see on the repository

I specify wicket version 1.5-RC7 in my pom.xml. When I do a mvn install it complains about: The following artifacts could not be resolved: org.apache.wicket:wicket:jar:1.5-RC7, javax.transaction:jta:jar:1.0.1B. So I try searching for the javax.transaction.jta-1.0.1B.jar on mvnrepository.com, and I am able to find it. Why does maven tell me it can't resolve avax.transaction:jta:jar:1.0.1B?
My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
…
<repositories>
<repository>
<id>mvnrepository</id>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>
<dependencies>
<!-- WICKET DEPENDENCIES -->
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket</artifactId>
<version>${wicket.version}</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-spring</artifactId>
<version>${wicket.version}</version>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-extensions</artifactId>
<version>${wicket.version}</version>
</dependency>
…
</dependencies>
</build>
<properties>
<wicket.version>1.5-RC7</wicket.version>
...
</properties>
</project>
First, http://repo1.maven.org/maven2 is the default one that Maven will lookup. You don't need to declare it again.
Can you check, when you build, which server your Maven is trying to connect? You should see something like Downloading: http://whatever.com/path/to/dependencies.pom.
I have just checked in maven central repo, there is no org.apache.wicket:wicket:1.5-RC7. You have better to check if you have declared the correct dependency
For JTA, it is a bit tricky. For quite some Java spec JARs, because of licensing issue, the actual JAR is not available in the public central Maven repo. If you look at http://search.maven.org/#artifactdetails|javax.transaction|jta|1.0.1B|jar , you will see it only contains POM, but not the JAR.
There are some ways to solve:
If you have a Maven repo in your own company, consider getting the JAR from Sun/Oracle, and deploy it yourself
For JTA spec itself, JAR is available for newer version (1.1) . Check if it is fine for you to use to latter spec
Switch to use Geromino spec. It should be compatible. http://search.maven.org/#search|gav|1|g%3A%22geronimo-spec%22%20AND%20a%3A%22geronimo-spec-jta%22
Edit
I have missed the wicket 1.5-RC7 in my previous search. Sorry. After looking into the POM, it seems that it is not a JAR POM. org.apache.wicket:wicket:1.5-RC7 is of type POM, in which declares dependency to wicket-core artifact.
You have two ways to do:
Change your dependency to point to wicket-core (and maybe other wicket modules) instead of wicket.
Change your dependency declaration to have <type>pom</type> (because default is jar)
I am not familiar with Wicket, but I believe method 1 is preferred.
Check your ~/.m2/settings.xml, I'm guessing you have an internal company (Nexus) repo mirror/proxy setting avoiding you fetching from internet -- and this mirror is stale.
If this is the case there are two common way to fix it: fix your internal Nexus repo so it's up to date, or bypass the Nexus repo so you fetch from the internet
I had faced this issue just now and resolved it. I have seen lot of questions in stack overflow related to this . Since my resolution was slightly different I am posting this answer. .
Issue :-Failed to execute goal on project Apigee-Edge-deploy-plugin: Could not resolve dependencies for project io.apigee.build-tools.enterprise4g:Apigee-Edge-deploy-plugin:maven-plugin:1.0.0: The following artifacts could not be resolved:
Root Cause:- The Repo had the dependency jars mentioned in the project. But the dependencies in turn had other dependencies which was not present in the repo and caused this issue. I added exclusion to the dependency and it worked.
eg:-
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-metadata</artifactId>
<version>1.3</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>woden</artifactId>
<groupId>org.apache.woden</groupId>
</exclusion>
</exclusions>
</dependency>
Reference:-
Also checkout the very good article on maven dependencies and exclusion

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