Binary library dependencies in Maven / Eclipse - java

I'm new to Maven and trying to work out how to deal with a situation where I have a set of binary libraries (.jars) that I want to include in multiple Maven-managed projects.
The libraries don't have a pom.xml file and aren't available in any Maven repository.
How should I set up Maven / m2eclipse to include these libraries in my other projects? I'm assuming I need to set up some kine of Maven "wrapper project" to deal with these?

If you're team programming use #limc post.
If it's just for you, there are a couple of ways of dealing with it:
There's a maven plugin to add the jar to your local repository.
mvn install:install-file -DgroupId=<your_group_name> \
-DartifactId=<your_artifact_name> \
-Dversion=<snapshot> \
-Dfile=<path_to_your_jar_file> \
-Dpackaging=jar \
-DgeneratePom=true
Or you can reference the jar file as a system dependency like so:
<dependency>
<groupId>com.package</groupId>
<artifactId>id</artifactId>
<version>5.2</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/my.jar</systemPath>
</dependency>

You will need to first install the jars into your Maven repo shown by #Will's post. Keep in mind, these jars only exist in your Maven repo. In another word, if you are sharing this project code with fellow developers, they have to do the same thing on their own local repos, or else they won't be able to find the jars.
A more elegant solution to team development like this is to host Nexus in some local server. What Nexus does is it groups all the external repositories into one spot (think of it as a proxy). So, you will want to install that jars in Nexus under "hosted repository" (there are 3 types of repo in Nexus, by the way). Then, configure all your team member's development to pull from Nexus instead of the internet. This way, when your team member checks out your project into their local workspaces, everything will be seamless and they will get the jars automatically too. You don't need to configure external repositories in your pom.xml this way. :)

Related

Add project repo for an edited jar in Eclipse Maven project

I have a legacy project that I'd like to convert to a Maven project for dependency management.
The problem is, that I have one jar (fop-1.1.jar) that I had to edit. It differs from the one that is publicly available and I only have it locally. But I need it this way.
What I tried to do, following several similar how-to's, it to create a fake Maven repo inside the project (local repo is no good, because several people work on that project and the solution has to be self-contained on Git) and reference this repo from the pom.xml. Sounds like the way to go for me, but it doesn't work. Eclipse show the project repo grayed-out :(
What am I missing?
BTW: this is what I tried to follow: https://devcenter.heroku.com/articles/local-maven-dependencies
Let me suggest another way: When we need to "edit" a jar, we give it a special version number like 1.1-edited instead of 1.1.. Then we can easily upload it to our normal Maven repository and use it. Maven even makes sure that you do not accidentally load both versions in the same project because the edit is only in the version number.
I guess what you need is a private maven server(I guess it exists), and then execute command to deploy jar( before deploy, check your account has privileges)
mvn deploy:deploy-file -Dfile=${jarFilePath} -DgroupId=${groupID} -DartifactId=${artifactId} -Dversion=${version} -Durl=${privateServerURL} -Dpackaging=jar -DrepositoryId=${privateServerURLInYourMavenSettings.xml}
,
after deploy successfully, add maven dependency
<dependency>
<groupId>${groupID}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
</dependency>

Uploading sources to nexus makes snapshot dependencies unavailable to maven

When we do mvn install deploy to a repository ${repository-jars} these dependencies can be used by others as intended.
When we afterwards upload sources to another repository ${repository-sources} the dependencies are no longer resolved correctly.
mvn deploy:deploy-file
-DgroupId=foo
-DartifactId=bar
-Dversion=1.0-SNAPSHOT
-Dfile=target/bar-sources.jar
-Dpackaging=jar
-Durl=${url}
-Dclassifier=sources
-DrepositoryId=${repository-sources}
Everything looks correct in nexus and both repositories are available in a regular Repository Group.
The hypothesis is that maven tries to use the latest uploaded artifact and ends up downloading the sources jar instead somehow ignoring the classes modifier. This is verified by first uploading sources and then do a maven deploy where it works as intended.
What is the correct way that allows us to upload sources in a separate job that runs after a deploy?
In examination, Maven is creating two different snapshots, and when you attempt to get latest, it's getting the newest one (which is sources) from the Group you've setup.
What you might actually try is putting them in the same repo, and separate them using Content Selectors. This is our newer version of Repo Targets from Nexus Repository 2.

How to build a Java Maven project without internet [duplicate]

This question already has answers here:
How do I configure Maven for offline development?
(15 answers)
Closed 7 years ago.
I'm trying to convert a java project into a Maven project with the Eclipse plugin. However my work environment does not allow any internet connection whatsoever and I cannot compile the code outside of the work environment. I have listed all the Referenced Libraries (which im guessing are the dependency libs) in the original Java project which I was hoping Maven would take from rather than connecting to the internet. Is there a way I can build the Maven project without connecting to the internet>
You need an internet connection. Maven isn't initially self-sufficient. It needs to download a bunch of plugins along with their dependencies and the dependencies of your own project. And this really depends on what sort of settings you have for your projects. One set up will require one set of dependencies, another - a whole different one. You can't download artifacts from the Maven Central manually and then install them locally one by one. Simply put, that sounds stupid.
I understand that you're coming from the Ant world where Ant has everything it needs on the local file system. However, Maven relies on the fact that it will have a central repository (either Maven Central, or your own repository - Nexus, Artifactory, etc.) from which to download the plugins and dependencies it needs. There is no point in you migrating to Maven, unless you'll be allowed access to the Central Maven Repository.
Yes, indeed, you can run Maven offline and you can have Maven produce a local repository for you to use when you are in offline mode. However, what you're trying to do is against Maven's principles.
If your company won't allow access to Maven Central, just stick to Ant. Your effort will be a waste of your company's and, ultimately, your own time.
you can add every jar to maven repository using mvn install command, then use dependency for only added jar. if you use plugins you need do some works for them..you use maven for 1 time with internet then use -o to use maven offline(mvn -o install).
mvn install:install-file -DgroupId=com.example -DartifactId=example -Dversion=1.0 -Dpackaging=jar -Dfile=/path/to/jarfile
<dependency>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<version>1.0</version>
</dependency>

Maven dependency issue - artifact not found in central repo

I'm trying to build the project from this site http://www.joptimizer.com/usage.html. I downloaded the sources jar file, unpacked it and ran maven package in the root folder. Maven fails at the last minute saying it couldn't resolve the dependency..
could not find artifact seventytwomiles:architecture-rules:jar:3.0.0-M1 in central repo - repo.maven.apache.org/maven2 ..
I have a feeling I might need to change something in the pom.xml file for this to work, but have no idea what. Googling for this missing dependency lead me no where. In general, how would one know what to do to handle such errors (and also please help with this specific case).
Specifically
According to the Building notes on http://www.joptimizer.com/usage.html:
JOptimizer is build on maven 3.0. Before building it, you must resolve
(in pom.xml) the external dependency on Colt and other dependencies
that aren't in public repositories. Please refer to the "Dependencies"
report for a complete treatment. For ease of use a boundle with
these external libraries is provided (visit "Download"): extract the
boundle in a folder and run the "maven-install.cmd" (translate it in
your own shell language), and you will get the artifacts in your local
repository.
To get the bundle for this, go to http://sourceforge.net/projects/cvxopt/files/, and download the appropriate version of joptimizer-3.X.X-dependencies.zip. Unzip in your own folder, and run mvn install:install-file -DgroupId=seventytwomiles -DartifactId=architecture-rules -Dversion=3.0.0-M1 -Dpackaging=jar -Dfile=architecture-rules-3.0.0-M1.jar -DpomFile=architecture-rules-3.0.0-M1.pom
Generally
Use a tool like http://mavenrepository.com to search for another version of the missing dependency and update your POM with the proper version. If MVNRepository doesn't know about it, you can install the dependency yourself. If you are working with a group of developers, as Eric Jablow mentions, an artifact repository like Nexus or Artifactory is great for sharing non-public dependencies. If it's just you, you can install the artifact in your local repo as described here: How to manually install an artifact in Maven 2?
You should add your own repository manager like Nexus or Artifactory. Then, find out where this dependency is kept; there are repositories other than central. If it's kept on another repository, have your repository mirror that too.
Otherwise, Nexus or Artifactory have commands to enter the dependency manually. Create a local repository called "Third-party" and add it there.
Finally, change your settings.xml file to refer everything to your repository manager.
The most common case for this is when a company refuses to license their products to be held at the central repository. For example, Microsoft won't let its sqljdbc.jar file be distributed through Central. So, you need to add it by hand.
Change the dependency as follows
<dependency>
<groupId>org.architecturerules</groupId>
<artifactId>architecture-rules</artifactId>
<version>3.0.0-rc1</version>
<scope>test</scope>
</dependency>
Add the repository in pom
<repositories>
<repository>
<id>architecturerules.googlecode.com</id>
<url>http://architecturerules.googlecode.com/svn/maven2/</url>
</repository>
</repositories>

Embed local jar files in maven project

I have some local jar files from a non-maven project which I wish to include in my maven-based eclipse project.
These jar files are undergoing a lot of change as me and my project buddy attempt to 'fix' them, so I'd prefer not to upload them to a repository to avoid making a maven version of this non-maven project if this is possible.
Of course, the jar files need to be embedded in the resulting deployment jar. We did this before using Ant which let us specify that those jar files should be included.
How do you do the same thing in maven? Take into consideration that we do have maven dependencies too which all work fine and aren't required in the deployment. Some answers I've seen don't allow for this requirement.
Here's one of my attempts - the problem is that the jar does not get embedded:
<dependency>
<groupId>se.krka.kahlua</groupId>
<artifactId>kahlua-core</artifactId>
<version>5.1_2.1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/kahlua-5.1_2.1.0-core.jar</systemPath>
</dependency>
System paths are a very bad idea. When anybody else checks out your projects, he cannot build it anymore. (I always see such crap in many companies). The right solution would be to install the jar into the local repository:
$ mvn install:install-file -Dfile=[JAR NAME] -DgroupId=[GROUPID OF
JAR] -DartifactId=[ARTIFACT OF JAR] -Dversion=[VERSION OF JAR]
-Dpackaging=jar
In your project, you just add the dependency as usual after you installed the jar into the local repository.
<dependency>
<groupId>[GROUPID OF JAR]</groupId>
<artifactId>[ARTIFACT OF JAR]</artifactId>
<version>[VERSION OF JAR]</version>
</dependency>
You can use maven-install-plugin to install kahlua-5.1_2.1.0-core.jar into the local repository then this dependency will behave as any other, see http://maven.apache.org/plugins/maven-install-plugin/usage.html. Or make a remote repository in a location shared with your buddy and let him upload his jar there with maven-deploy-plugin:deploy-file (http://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html) each time he changes it and add this repository to your pom. You can use SNAPSHOT version if this jar changes often

Categories