multiple repositories with the same artifact in maven - java

I have made some modifications to log4j and would like my project to use my local version rather than that from the remote maven repo, so I declared my project as a local repo in pom.xml in addition to my remote repo for other dependencies:
<repository>
<id>my-log4j</id>
<name>my log4j</name>
<url>file:///...</url>
</repository>
<repository>
<id>remote</id>
<name>remote repo</name>
<url>http://...</url>
</repository>
maven copied the files from my local repo as expected, but then it downloaded log4j again from the remote repo and overwrote the earlier files. Is there a way to exclude certain artifacts from being downloaded from the remote repo?
Also, how does maven detect changes to my-log4j? How can I make maven copy the my-log4j artifacts each time during compilation?

If you make a custom version of something, you give it a custom version number.
For example, if you modify log4j-1.2.17 for your own use, give it the version 1.2.17.JRR.1 and following numbers as you work on it.
You build them on your computer and when you run the install goal, it will put them in your local repo. If you have a shared repo for your group, it can be deployed there as well and never confused with the Apache releases.
This will never be found in the remote repo, just in yours.

If maven looks for artifacts, it always looks in your local repository first, you do not have to specify it (you can specify the location of your local repository in your settings.xml).
You answered your question already: If you had to change a third-party artifact, rename it (already in the pom.xml) like my-log4j or log4j-my-patch. Then it won't collide with the original artifacts.

Related

Maven: How to check if jar is not uploaded in central repo

How can I be sure that my jar files ARE NOT loaded to central repo maven? I am asking this question as I saw several times exception like - error while uploading to central repo. I was shocked (as I didn't make any configurations in pom and not applied to central repo administration). That's why I decided to ask this question.
So, how can I check that the absence or presence of some code guarantee that my jar is not uploading to central repo?
You can specify which repository your project should be deployed to via the Distribution Management section of pom.xml. I think there is no default. However, it's possible that you have a parent pom.xml specified and it contains some setting. If that is the case, you can modify parent. Failing that, you can override it by putting your own private repository details in this section to avoid deploying your artifacts anywhere else. It can even be invalid URL, in which case deployment will simply always fail.
Example:
<distributionManagement>
<snapshotRepository>
<id>fake-snapshots</id>
<url>https://fake/snapshots</url>
</snapshotRepository>
<repository>
<id>fake-releases</id>
<url>https://fake/releases</url>
</repository>
</distributionManagement>
Check whether are you using deploy:deploy-file goal under maven-deploy-plugin in your POM.xml.
This feature is used to deploy jar files to the remote repo.

How to import a deployed jar "SNAPSHOT" from local Artifactory repository into another project?

I am a newbie in Artifactory, I have 2 projects one depends on another...
I set up Artifactory on a server and deploy the first jar into libs-snapshot....and change the C:\Users.m2\setting.xml and add this tag in the pom of the deployed project:
<distributionManagement>
<snapshotRepository>
<id>serverId</id>
<name>serverName</name>
<url>serverUrl/artifactory/libs-snapshot/</url>
</snapshotRepository>
</distributionManagement>
how can I access the first project from the second one via Artifactory repository
I am working on Netbean8.2, glassfish 4 and artifactory 4
By default, maven doesn't know to look anywhere except your local repo and maven central. You'll need to tell it the additional repos it can look in either via a pom setting or settings.xml.
You can see some example and additional details in the Maven docs.

How to configure Maven2 to publish to Artifactory?

Currently I have a Maven2 project that builds a JAR when you run:
mvn clean package
I need to now tweak the pom.xml to publish this JAR (myapp.jar) to an Artifactory server running at:
http://myartifactory/artifactory/simple/myorg/myapp/0.1
I tried adding a <repositories> element to my pom.xml but nothing is being published with this config:
<repositories>
<repository>
<id>myartifactory</id>
<url>http://myartifactory/artifactory/simple/</url>
</repository>
</repositories>
Any ideas as to how I could get publishing to work? For simplicity's sake, pretend that this Artifactory repo is authenticated to accept publishes/writes from a user with a username=foo and password=bar.
You have two options (please note that the later is the recommended one):
Add DistributionManagement part to your pom and server part to your settings.xml
Let's say you want to deploy to libs-snapshot-local repository. In this case you need to go to the tree browser in Artifactory, focus on the repository level, copy the Distribution Management snippet and paste it in your pom.xml:
Next, you need to tell maven the credentials. For that, click on your username in the top right corner, enter your password to unlock the screen, copy the server tag from Maven Settings panel:
This one you paste in your settings.xml. Don't forget to replace the ${server-id} with the real server id (the one you have in Distribution Management now).
Now, just run mvn deploy and enjoy.
Working with Maven Artifactory Plugin:
Add the relevant <plugin> part as described in the wiki to your pom.xml. It includes both the target repository and the credentials (please use external credentials source, like environment variables or system properties).
Run mvn deploy and enjoy not only the deployment to Artifactory, but also additional features as described below.
Additional features of Artifactory Maven Plugin (on top of regular Maven deployment):
Allow adding custom properties to the deployed files
Provide the build bill of materials (the buildInfo), allowing Build Integration with any build server (even those not supported by JFrog) or even with standalone builds (without build server at all).

Download maven project dependencies to build offline later

I've used maven for a while in common way with settings out of the box, so I understand what it is but I'm newbie in maven settings.
I need to organize such workflow:
Developer writes java code, use some dependencies from internet.
Developer commits his work.
TeamCity automatically can build his work. Without any manual work, and without internet.
I have idea how to do it:
Developer uses maven. A "common" directory acts as repository for certain java projects.
After the work is complete, the developer commits his project and common directory into svn.
TeamCity updates project and common directory from svn and run "mvn package". Anything needs takes from common directory. Without worrying about internet connection and startup nexus, or other repo services.
My question is:
How to use simple directory on filesystem as proxy repository for certain projects?
Tell me please how to realize this idea or give me another idea to realize such workflow.
I can just commit local repository, but there are some limitations:
Local repo zip artifacts. If I make even little changes to it - the whole cache file must be uploaded and downloaded to/from svn. It takes a long time.
Local repo store artifacts for all projects. I want only certain projects to use this repo, because developers don't want to check changes and filter unused dependencies.
I test local directory to deploy projects, simple by writing "file://testRespoDir" in repo url, but I can't understand how to make this directory proxy all remote artefacts for project(project must not use local repo and use only common directory.
I found simple and perfect solution:
POM include 2 repositories:
<repositories>
<repository>
<id>commonDep</id>
<name>common dependency</name>
<url>file://../common/repository</url>
</repository>
<!-- it used when collect dependencies before commit. If developer already download dependency, get it from local repo, save traffik and time -->
<repository>
<id>localPlugins</id>
<name>local plugins</name>
<url>file://${user.home}/.m2/repository</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>commonDep</id>
<name>common dependency</name>
<url>file://../common/repository</url>
</pluginRepository>
<!-- it used when collect dependencies before commit. If developer already download dependency, get it from local repo, save traffik and time -->
<pluginRepository>
<id>localPlugins</id>
<name>local plugins</name>
<url>file://${user.home}/.m2/repository</url>
</pluginRepository>
</pluginRepositories>
when developer opens project, he use local, common and central repositories in such order. It saves his traffic. When he finished work, he call script:
mvn dependency:go-offline -Dmaven.repo.local=../common/repository
all current dependencies of project copyed from his default local repository to common repository. Then developer commit common.
When we run build on teamcity, we checkout project and common and run it. There is no internet connection, but all dependencies exist in common repository.
Yeah you can do this personally i wouldn't recommend it especially if you're using SNAPSHOT's however you should be able to.
So what you want to do is create a network drive (i dont know whether your on windows or linux but it dont matter).
Then mount that network drive on all systems which require it.
Then in maven config file specify the local maven repo location:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd" >
<localRepository>c:/mvn-repo/</localRepository>
...
</settings>
Replace c:/mvn-repo/ with path to your the directory on the network drive you wish to use.
You can place this config in various places but i would suggest placing it in the root config file which lives # ${MAVEN_HOME}/conf/settings.xml.
You will need to specify this on each computer which is using maven.
Then that should do it and all your maven run times will share the same local repo.
So how to get round different projects using different directories? Thats a tricky one you could use different directories on network drive and change the localRepository variable # run time by specifying it as a runtime property.
mvn -Dmaven.repo.local=$HOME/.my/other/repository clean install
That way you would have it all parceled up nicely one network drive with a directory for each project then simply specify that variable # run time to set which local repo to use.
The flow you propose won't scale. I would rather set up a local corporate mirror of the central repository and have both developers and automation servers (teamcity etc.) use it. Trivial to set up, easy to maintain, easy to track dependencies on the third party software and put restrictions in place.
I want to try such solution:
Add repository file://testRespoDir to POM.
In pom there is plugin in init, which copy project dependencies to this repo, if such dependencies not exist there.
Disable Central on teamcity server.
Only one question - which plugin can copy dependencies in such way?

How to add javax.* dependencies in Maven?

I am getting tired of manually installing javax jar files in Maven and would like to know what is the best solution to include a dependency on javax.cache, javax.transaction, or other JSRs that are not easy to find in Maven repositories.
Have you seen https://people.apache.org/~ltheussl/maven-stage-site/guides/mini/guide-coping-with-sun-jars.html ?
This link suggests groupID and artifactID's to use, as well as a java.net repository.
It looks to me like almost all of these exist in the central Maven repository under this naming scheme.
I'm not aware of one, but adding the java.net repository may help you with some of these dependencies:
<repositories>
<repository>
<id>java.net repository</id>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
If building on more than one box and/or for team development, a local (intranet) maven repository manager can help with these "missing" jars. This centralizes the configuration and management of not only 3rd party jars that are not in a public repository, but also all external repositories in general. It could also help automate your builds, creating more 'reproducable' builds (e.g., if you have a pool of continuous integration servers).
install a mvn repo mgr (see list -- imo, nexus is really simple to start with);
use a custom settings.xml that includes a "mirrors" section pointing to your intranet mvn repo mgr. Either update your ~/.m2/settings.xml, or run maven with "mvn -s etc/settings.xml"-- useful for hudson builds, where you don't want a custom per-user settings.xml;
manually upload your 'problem' jars to your internal repo (again, super-simple w/ Nexus via a web-interface);
set up the internal mvn repo mgr as a "mirror" of repo1.maven.org/maven2, codehaus, java.net, ... (etc).
Now, you can centrally define all 3rd party repositories & 3rd party jars -- rather than requiring each person, each box and/or each project define them individually in their pom or settings.xml. Each project / person / box would ONLY define your central, internal maven repo as the single repo for all maven projects.
This also really speeds up your artifact re-download time for fresh builds, or for those times when you need to (or would like to) delete your local ~/.m2/repository cache.
Repo managers: nexus, archiva, artifactory... e.g.,: maven.apache.org/repository-management.html
- http://docs.codehaus.org/display/MAVENUSER/Maven+Repository+Manager+Feature+Matrix
javax.cache are in jcache:jcache:1.0-XXX artifact (in Maven's central repo)
<dependency>
<groupId>jcache</groupId>
<artifactId>jcache</artifactId>
<version>1.0-dev-2</version>
</dependency>
javax.transaction.* classes are in javax.transaction:jta:1.1 artifact, JTA jar can’t be inserted in the Maven repository because the Sun’s Binary License (I know, this sucks). To use it you need to download manually the JAR (it's free) and put it into a local repo or use 1.0.1B version which is contained in java.net.
NOTE: I've read in some place JTA will be integrated in future versions of the JDK
I know is really a pain to find these artifacts in Maven's repositories but you can make a search of a class in www.mvnrepository.com and it will show you the correct groupId and artifactId for mostly all the packages.
In the particular case of JTA, I hit this post:
http://www.jugpadova.it/articles/2005/11/26/maven-2-spring-and-jta-depencies
.. which makes sense, if I didn't have to spend a lot of time in Oracle's horrible site to get the forementioned JAR file. (I was an Oracle's enthusiast myself but that site could use a lot of UX rework here and there).
I decided to replace the dependency with what Hibernate provides, via Geronimo, as per this post (worked perfectly):
https://forum.hibernate.org/viewtopic.php?p=2420836
The deal with Java licensing and Maven is currently being worked on by the Hibernate team, or so it seems here:
https://hibernate.onjira.com/browse/HHH-4548
Thanks to everyone for sharing!

Categories

Resources