Maven and m2e - build jar with dependencies information - java

We are using Maven and m2e tools for our development and today we encountered a problem.
One of our projects is small library that is required for other projects, so we packaged it into jar file and put in our private Maven repository.
For now, all of the jars that we put in this repository didn't have any external dependencies, but this library I mentioned uses some external jars.
Now, when I add information about this jar to other poms, this jar is downloaded from our private repository but Maven doesn't download dependencies needed by this jar.
I am wondering if I need to use some special target/add something to my pom.xml file that will inform Maven to include information about dependencies needed by this artifact.
EDIT:
Here is the workflow I perform when I upload jar to our private repository:
1.I generate jar file from Eclipse using m2e
2.mvn install:install-file -DgroupId=<your_group_name> -DartifactId=<your_artifact_name> -Dversion=<snapshot> -Dfile=<path_to_your_jar_file> -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true
3.I copy folder created in my local repository to remote repository

If your small library is a maven project as you state, there should be no reason to have eclipse build the jar and then use maven to install it and then manually copy to the remote repo. Instead you should use m2e to run the deploy goal:
mvn deploy
That will cause the jar to get built and then install it directly into your local maven repo then deploy it to the remote repo.
In eclipse this can be accomplished by right clicking your project, choosing Run As -> Maven Build... then in the run configuration window for Goals input type deploy then click Run. After this has been done once, you can just use Run As -> Maven Build to run the same config again.

I see you use -DgeneratePom=true during the installation of the jar file. What you need to do is create a pom.xml for your artifact. In the pom.xml, you can specify the dependencies that your jar file requires. When executing the install:install plugin goal, you use -DpomFile=pom.xml instead.

The best way to do this is to run mvn deploy
You have to setup the distribution repository to your private artifact manager (nexus or artifactory) in your settings.xml
see this for more details

Related

Maven install fails after maven clean in eclipse

I have a maven project in Eclipse and I added some local jar files to the buildpath. If I do not add any dependency to the pom.xml file I am able to execute maven install. Then, if I add those dependencies to the pom the command maven install continues working as well. Now in this situation if I run maven clean then maven install fails. Why?
I also tried to run Maven -> Update Project but the result is the same. What is the problem?
If you are using non maven dependencies then it will fail to build eg from CLI and in your case in Elipse after cleaning the project as well. In order to make it work you have to installl tha JAR you are using as Maven artifact and the ninclude it in POM dependencies like every other library.
Here you have info on how to install 3rd party JARs to local repo
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
I got the same problem and resolved by adding the 3rd party in the pom.xml manually

Multiple local jars needed for my maven project but should work for others too

I have a Java maven project that depends on 50 jars that is not in Central Maven Repo or in my organization's Nexus repository. I don't want to do mvn install:install-file ... as it will work for me but it won't work for others unless they also run this command and install all the jars in their local repo.
I can put all my jars in project lib folder and check it in. But I need to hard code the path in the pom.xml for these jars.
Is there a way to handle this?

Showing jars in maven dependencies section without doing mvn install in cmd

I have added dependencies in pom.xml and immediately the corresponding jars started to show up in maven dependencies section of dynamic web project.
I just want to know that I have not done mvn install in cmd so how did they get saved in maven repository.
Another query I have, is that since jars are availble in maven dependencies folder of dynamic wep project, so my project runs successfully or not as depndencies are already satisfied without doing mvn install.
When you list a <dependency> in your project's POM, M2Eclipse (Eclipse's plugin in this case) will trigger Maven to resolve that declared dependency...meaning Maven will check your local repo first for that dependency, and if it's not found there it will pull it down from the next highest repo you have configured (possibly an agency-level repo, or Maven's default public repo on the web).
No mvn install is required, as the purpose of that would be to install your current project's packaged artifact into your local repo, rather than install any dependency.
Hope this helps to clarify why an install is not used to copy dependencies into your local repo.

How to organize local dependencies with Maven?

in my company, I have a Java project:
common: This contains classes that are used for several projects
GUI: This project contains a GUI for common
Without Maven, I add common to the class path, I can build the project. But how to setup the POM.xml and the Eclipse workspace when I want to build the GUI with Maven?
I also want to package the app into a JAR later, so I tried to setup the gui as .jar package
But then how to assign a Maven type to the common project?
Ideally, I could import "common" to the Maven project?
UPDATE:
Ok, it seems the mvn package command is able to resolve the "common" project when I add as local dependency see this under GUI. Still a bit confused about whether to use "pom", "maven-plugin" or "module" - anyone can add some links/clarifications, when to use what approach?
Declare common as usual maven dependency in GUI.
If it isn't maven project, add it to local repository as shown there How to add local jar files in maven project?
I would follow this steps
Create a local maven repository to store your custom jars. Which nothing but a proper directory structure with pom files in it.
Create your sub projects(common, gui) and package them as jar.
Install the local jars to local mvn repository.
Use the jars as dependency from your project.
Example:
Create a master project on ${master_project} location and your subprojects on ${master_project}/${common} and ${master_project}/${gui} location.
Create mvn repository in: ${master_project}/local-maven-repo
Specify your local repository location in your subprojects pom.xml located in ${master_project}/${gui}/pom.xml
<repository>
<id>local-maven-repo</id>
<url>file:///${project.parent.basedir}/local-maven-repo</url>
</repository>
Install your jars in local mvn repository
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where: <path-to-file> the path to the file to load
<group-id> the group that the file should be registered under
<artifact-id> the artifact name for the file
<version> the version of the file
<packaging> the packaging of the file e.g. jar
Finally use them as regular dependency
<dependency>
<groupId>org.company</groupId>
<artifactId>common</artifactId>
<version>0.9.0-SNAPSHOT</version>
</dependency>
Reference for this answer.
Here's further resources for Maven multiple module projects
install the maven plugin in your eclipse using the following link.
This will enable maven in your eclipse. Then right click on your project and click Configure and you will see Convert to Maven Project option. This will convert your existing project to a maven project. You can configure your project to be build as a .jar, .war etc. while converting from the wizard. The following is the tutorial on how to do it... link

Download a Maven artifact and install into a specific local repository (not .m2)

I have several local Maven repositories besides the one located in ~/.m2 directory and I want to simplify the process of installing new artifacts into them from Maven central.
So far I couldn't find a way to tell mvn dependency:get that dependency should be put into a specific local repository.
I did manage to find a way to install a given downloaded artifact using mvn install:install-file -DlocalRepositoryPath=, but I want to be able to get and put dependencies into a specific repository with as few manual steps as possible.
Before you ask why can't I just configure my project to use Maven central directly here is the answer: the project uses Gradle and I do not own its build script (i.e. I can't modify it). The project build script is written to work with several distinct repositories having the same base URI that I fortunately can change using build.properties file. So my idea is to have several local maven repositories in the same root directory and trick the build script to use them.
You can use the maven.repo.local property:
mvn dependency:get -Dmaven.repo.local=/path/to/localrepo

Categories

Resources