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

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.

Related

mvn package ignoring repository in pom.xml

We cannot get a project to pull from a remote repository using mvn package. In maven installed folder, config/settings.xml we declare our internal central repo:
<mirrors>
<mirror>
<id>advnexus</id>
<mirrorOf>*</mirrorOf>
<url>http://internalserver/nexus/content/groups/public</url>
</mirror>
</mirrors>
This works for all of the jar files hosted at this repo. But we have some additional jar files we pushed to a server under our control and running Sonatype Nexus. So in the project's pom.xml, we setup our remote repo as:
<repositories>
<repository>
<id>companynamenexus</id>
<name>Company Name Project Repo</name>
<url>http://nexus.companyname.com:8081/repository/project-name</url>
<layout>default</layout>
<spanshots>
<enabled>false</enabled>
</spanshots>
</repository>
</repositories>
When I run the build, it pulls any needed jar files from the main repo, but simply times out trying to pull the jar files that don't exist on this repo vs. using the repository in the pom.xml.
Note that in the settings.xml mirror section, I tried changing the <mirrorOf> to be central vs. *, but this just caused additional errors.
I have also tried setting up this repo as a secondary mirror in the settings.xml, tried commenting out the mirror in the settings.xml and place it as an additional repository in the pom.xml, all without finding the right combination. Seems like a simple problem, but the answer is eluding us.
Best solution: Don't try to access two different internal Nexus, but set up a repository group in one of the Nexus that contains all other repositories (may they be hosted or proxy, internal or external). Then you can simply set a mirror entry on that repository group and you are fine.
Second best solution: Change your mirror entry to <mirrorOf>*,!companynamenexus</mirrorOf> and define the additional repository in the <repositories> section of your settings.xml.

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.

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?

multiple repositories with the same artifact in maven

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.

How can I make the dependencies not upload to the company 's internal repository?

My pom.xml use the following code to define the company 's internal Maven repository such that the dependencies will be downloaded from this repository if they cannot be found in my local repository.
<repositories>
<repository>
<id>XXXXXX</id>
<name>Internal Repository</name>
<url>http://private.ip/nexus-webapp/content/groups/public/</url>
</repository>
</repositories>
When I add some dependencies in pom.xml , I find that the dependencies I added will also be added to that internal repository . Besides deleting <repositories> section in pom.xml , can I configure its attributes such that the dependencies added in the pom.xml will not be added to this internal repository?
It sounds like what you're talking about is Nexus' proxying mechanism. You request artifacts from Nexus, and it looks at configured outside repos for the artifacts, caches them locally and returns them to you. That assumes the repositories in question are configured to be proxied through Nexus, of course. If someone set it up that way, then why do you want to circumvent it? You'd use Nexus in this way so the artifacts are closer to you and your builds work faster. The only way you'd get this not to happen is to change the settings in Nexus or else stop using it. You don't have to remove the repo entirely from the pom. Just put other repos ahead of it, and Maven will look in those first. But again, why would you not want to use Nexus as it was designed as a near cache for artifacts?
You need to configure it in your repository software (Artifactory, Nexus, ...).
I think you have set up a proxy repository here which downloads every artefact requested. You might want to try running a 'hosted repository' instead. More info here.
The equivalent concept in Artifactory is a 'local repository' (read here).
Download and install the dependencies you need manually using following command. It will add the package to your local repository such that you can use it. Read here
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

Categories

Resources