How to resolve maven dependency explicitly from command line arguments? - maven

I have a project A which depends on artifact B. I made some hack on B, and want to see it in A. So I don't want A to use the version in my local repository, instead, I want A to use my hacked version of B.
I'm looking for a solution that can specify my-hacked-B.jar as dependency of A, like this:
cd A && mvn package -Ddependency.org.groupB.B.jar.path=path/to/my-hacked-B.jar
Is it possible, or I have to install the modified B in my local repository?

You can't do it on the command line, but you can set the dependency scope to system in the pom.xml and provide a path to the dependency.
<dependency>
<groupId>org.groupB</groupId>
<artifactId>B</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/path/to/hacked-B.jar</systemPath>
</dependency>

There is no way to resolve dependency from command line, but there is a way to let maven resolve dependency externally.
I created a temporal wrapper pom.xml which aggregates the two projects. It works, without install anything into local repository.
The only problem is that I am unable to use absolute path in <module/>.
See http://maven.apache.org/pom.html#Aggregation

A little bit late :) You can do it with properties:
<properties>
<org.groupB.B.scope>compile</org.groupB.B.scope>
<org.groupB.B.path></org.groupB.B.path>
</properties>
<dependency>
<groupId>org.groupB</groupId>
<artifactId>B</artifactId>
<version>2.0</version>
<scope>${org.groupB.B.scope}</scope>
<systemPath>${org.groupB.B.path}</systemPath>
</dependency>
and then:
mvn package -Dorg.groupB.B.scope=system -Dorg.groupB.B.path=path/to/my-hacked-B.jar

You could mvn clean install your hacked version into your local maven repo, over the version you downloaded from the 'net. You'd need to make sure that project B's pom reflects the version you are depending on and not a snapshot version. (Or, to put it more generally: The version of project B that you mvn clean install must match the version you require in project A. Whether you edit A's pom or B's pom doesn't matter.)

Related

IntelliJ How to force downgrade dependency version?

I have a persistent problem with maven dependencies version changes in IntelliJ. Whenever I try to use a previous version of a library and change the dependency version in my pom.xml nothing happens. Maven continues to use the newer version of the library.
For example I want to use:
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
But Maven repo has version 2.0.2 saved :
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
So for my projects version 2.0.2.RELEASE is used.
I tried reimporting the project first. Then I tried "reimpor all maven projects". Then I checked Settings > Maven > Always update snapshots. I also tried opening the project settings and deleting the dependency from there, but on reimport the 2.0.2 version will be imported in the project. For now the only thing that works is deleting manually the folder from the ".m2" folder.
Shouldn't library versions be strictly followed and shouldn't version 2.0.1 v be used for my project?
The moment you change the version of the artifacts, maven will use the same version. It will never use neither new version nor the older version. Since you are using intellij, you can check which are the jar files along with their version used. See below the screenshot.
You can expand the External libraries as shown below and you can check the dependencies used in pom.xml.
Besides, you can also check in command prompt. Go to command prompt and point to the project directory and type the following command.
mvn install dependency:copy-dependencies
You can see all the required dependencies along with version information in target folder.
I suggest you not to delete the .m2 directory as you may have to download all the dependencies once again.
If you want to enforce the use of a particular dependency version you can use:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
What this will do is exclude the dependency unless it actually gets used, and then if it does gets used it only uses the version you have specified.
Not clear what is the issue.
Repo can contain everything, no matter if dependency is present locally.
Also, Idea does not resolve dependency itself, we use maven api to resolve them.
By default, maven takes dependency which is nearest to root (see https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html)
Specifiying explicit dependency in root pom should force using this version.
Could you please provide mvn dependency:tree output and corresponding IDEA maven dependency diagram (if you have IU)?
If Idea resolve another dependency version than maven, please fill an issue at https://youtrack.jetbrains.com/issues

How to add dependencies to pom.xml in IntelliJ 14

I have a few dependencies in Project Structure/Libraries in IntelliJ 14. How can I add them to my maven pom.xml? There is one single tutorial on IntelliJ's website that does not work for me. I don't want to manage them manually.
The proper way to do this would be to install the dependency artifacts (most likely jars) into your local maven repo, like this.
How to install artifacts to your local maven repo
And then add the dependencies into your pom.xml
<dependencies>
<dependency>
<groupId>com.something</groupId>
<artifactId>artifact</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
Yes, this does require going through each artifact manually, one at a time, but it's a one time setup process.
That is the "proper" way. After that, you can do away with library dependencies in your project structure (they will be picked up correctly via maven).
There is the alternative possibility to "hack" in your project libraries path as a sort of "embedded" maven repo in your project, but that's a little bit hacky and I wouldn't advise that.

Use maven to "extend" existing jar file

My project contains a couple of class which have to be integrated into an final jar file.
So my current "workflow" looks like:
mvn compile && jar uf contribution.jar -C target/classes .
I guess this could be done with the maven-jar plugin or through maven-assemblies but can't figure out how.
Simple - add the element <packaging>jar</packaging> to your pom.xml and invoke the following:
mvn package
See Maven build lifecycle for more information. It really is a must-read if you're planning to use Maven for you project.
Edit
Thanks for the update, I know understand what you mean. There may be a better way to do this, but this is what I use:
Maven will automatically include this jar if you add it as a dependency in the usual way, i.e:
<dependencies>
...
<dependency>
<groupId>some.group</groupId>
<artifactId>contribution</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
The question is how to ensure that maven can find the jar? If the jar were in the Maven Global repository it would find it automatically, so always check this first. If not, then you will have to create your own repository to store custom jars. This may not be the most clever way to do it, but it's how I do it. Note that this is not the same thing as the cache stored in the .m2 folder.
Let's say we want to use c:\maven\repo as our local. To do this, first ensure the folder exists then add the following lines to your pom.xml:
<repositories>
<repository>
<id>my-repo</id>
<url>file://c:\maven\repo</url>
</repository>
</repositories>
Next, you will need to manually add the contribution jar to this repo. To do this execute the mvn deploy:deploy-file command and give it the appropriate values. See here for more information.
Now when build/compile/package your original project, it should include contribution.jar in the packaging as it is a dependency.
Hope this helps :)

Need explanation for maven error please

http://blog.bigpixel.ro/2012/07/building-cc-applications-with-maven/comment-page-1/#comment-8196
I'm following the example above for the maven nar plugin, but I get the following error when I do a mvn package
“could not find artifact net.sf.antcontrib:cpptasks-parallel:jar:1.0-beta-5-parallel-1-SNAPSHOT” but I see the following folder tree and its contents in my ~/.m2/repository... What gives?
~
.m2
repository
net
sf
antcontrib
cpptasks-parallel
1.0-beta-5-parallel-1-SNAPSHOT
Change dependency
<dependency>
<groupId>net.sf.antcontrib</groupId>
<artifactId>cpptasks-parallel</artifactId>
<version>1.0-beta-5-parallel-1-SNAPSHOT</version>
</dependency>
to
<dependency>
<groupId>org.codeswarm</groupId>
<artifactId>cpptasks-parallel</artifactId>
<version>20121119</version>
</dependency>
Unfortunately, neither maven-nar-plugin nor cpptasks-parallel are currently deployed to Central. So you need to mvn install them yourself (or better, mvn deploy them to your own Maven repository). You can find both projects on GitHub.
EDIT: nar-maven-plugin version 3.0.0 has been released, and is now available from Maven Central. Two notes:
The groupId and artifactId changed; the GAV is now:
<plugin>
<groupId>com.github.maven-nar</groupId>
<artifactId>nar-maven-plugin</artifactId>
<version>3.0.0</version>
</plugin>
The cpptasks-parallel project has been merged into nar-maven-plugin, so no need to worry about that dependency anymore.

about generate maven dependency

I am pretty new to maven.
Now I have a maven project developed. My another project needs to depend on this one.
Does anyone know how can I generate my own dependency? So that my second project can add the first one as a dependency in pom.
thank you very much
Since your first project is already a maven-project, just install it in your local repository by running mvn install in the first project's root directory.
Then you can include a dependency in your second project by simply referencing the groupId, artifactId and version you defined in the first project.
So if your first project had the following in its pom:
<project>
<groupId>com.yourdomain</groupId>
<artifactId>yourcomponent</artifactId>
<version>1.0</version>
... <!-- more here -->
you can include this in your second project:
<dependencies>
<dependency>
<groupId>com.yourdomain</groupId>
<artifactId>yourcomponent</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Unless you deploy your project 1 jar to a central maven repository, this will only work if your jar is in your local repository (via mvn install).
Maven projects are identified by the "Maven coordinates", that is, the ArtifactID, GroupID and version.
Say you create your first project and run maven install. Your local repository (in $HOME/.m2/) will now contain the compiled project plus whatever coordinates you put in there.
Your second project must now only depend on the said coordinates.
I would suggest googling a bit on maven. I made a tutorial a long time ago that might help you, even if the examples are a little simple. Here you go and good luck!

Resources