Maven Build Jar - java

I am working on Maven project and I have a jar app-client.jar which have dependency on app-core.jar. So I have a pom.xml for app-client.jar and that pom.xml has dependency of app-core so we added dependency of app-core in this pom.xml.
Now I wanna use the app-client.jar in my main project. Because this jar is build locally and not available at remote repository. So I did add the app-client and also specify the location repository where it will located.
as following..
<repositories>
<repository>
<id>repo</id>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>file://${project.basedir}/../lib</url>
</repository>
<repositories>
<dependencies>
<dependency>
<groupId>com.sample</groupId>
<artifactId>app-client</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
and also I put my jar as following
[My Module] / [com] / [sample] /[app-client] /[1.0]/app-client-1.0.jar
When I run mvn clean install I got error app-client's pom.xml not found. and build get failed. Usually when I use single jar then its working fine, but if I use jar having dependency with other jar it getting failed.
So how can I build my app-client jar and their pom so that it behave normal and also deploy app-core.jar too.

firstly when you are building app-client.jar build a fat jar which includes app-core.jar dependency.
Next copying app-client-1.0.jar into the specified location of local repo doesn't work, to add this jar into your local repo use this command mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>.

If you build app-client.jar with mvn clean install, then it will be installed into your Maven local repository (.../.m2/repository). Then any other project on the same computer can reference it in its pom without further information. So no <repositories> entry necessary.
If you want to work with multiple people on the same project, use a Nexus/Artifactory server to share the jars.
Solutions with lib folders and system paths are deprecated and cause trouble.

Related

How to make maven download dependencies from central repo. rather than downloading from remote company repo.?

So I thought of creating a spring boot project but on my company laptop. I downloaded the project from spring initializer and tried to execute on IntelliJ idea but got this error:
"java: package org.springframework.boot does not exist"
"java: cannot find symbol
symbol: class SpringBootApplication"
I ran maven clean and install cmds but still the same issue. Now I reckon this is happening bc maven is downloading dependencies from my remote company repository.
I remember saving a custom setting.xml file in .m2 folder which contains a custom repository.
So if I am right how can I make maven download from the central repo or solve this problem?
It sounds like you listed your own repository in your POM or settings.xml, like so:
<repositories>
<repository>
<id>vaadin-addons</id>
<url>https://maven.vaadin.com/vaadin-addons</url>
</repository>
</repositories>
Doing so, without also listing Maven Central, will cause Maven to only attempt to resolve from your own repository. You must also list Maven Central, like so:
<repositories>
<repository>
<id>vaadin-addons</id>
<url>https://maven.vaadin.com/vaadin-addons</url>
</repository>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
Maven checks repositories in the order they are specified. You cannot specify a repository per dependency, but you could create a new module with your dependencies and their specific repositories.

Create Maven Project with Nexus Repository OSS 3 [offline]

My problem is that I can't create a new Maven project after adding my Nexus repository to the .m2 settings.xml.
I've installed Nexus Repositoy Manager OSS 3.0.2 as my local Maven repository. I have a machine which is in offline mode and can not connect to the internet. What I can do is transfer data from an online machine to it thought. So I can deploy all necessary libraries on the online machine and just switch the /data folder later on.
Error after creating a new Maven project with Eclipse:
Could not calculate build plan: Plugin org.apache.maven.plugins:maven- >resources-plugin:2.6 or one of its dependencies could not be resolved: >Failed to read artifact descriptor for org.apache.maven.plugins:maven- >resources-plugin:jar:2.6
The maven-resources-plugin-2.6.jar is deployed/available on my Nexus Repository: Path org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.jar
My Maven settings.xml (partly):
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-test/</url>
</mirror>
</mirrors>
...
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
...
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
As you can see my repository is named "maven-test". Is it normal that the Nexus repositories are not browseable by their URLs (http://localhost:8081/repository/NAME_OF_REPO/)?
Is there any documentation which Maven libraries are mandatory to create a simple Maven project. The minimum amount of .jars?
What I've tried so far:
created a simple Maven project on a internet connected machine and downloaded all necessary Libraries with:
mvn dependency:go-offline. After that I deployed all .jars from my local m2 repository to Nexus (about 160 Jars - luckily I used a Shell script for the deployment). For the offline simulation I deleted my local repository and went offline. Now the error occurs after creating a new Maven project in Eclipse.
I'm using Maven 3.3.9 - Java 1.8 - Eclipse 4.4 - Mac OS
Please any help would be appreciated!
Thanks
[Edit] forgot to upload the .pom files to Nexus. Clean install worked for a existing project. Still got the error after creating a new Maven project
Run a mvn clean install on the project without using the nexus tool.
This will download all the required jars and poms to your local repository as specified in your maven settings.xml
Copy all of these files to your offline nexus installation.
The caveat with any new projects you create each time is that they may require new jars and poms that weren't used by the first project and so were never downloaded and copied to your Nexus install which will give you issues.

Maven: metadata xml files downloaded often from remote repositories

I'm using Maven to handle a Java project. I thought that Internet connectivity was only needed in the 1st compile to download the required libraries from the remote repositories, but I get several download messages whenever I compile code. Messages like these:
Downloading: http://repo.maven.apache.org/maven2/org/eclipse/core/resources/maven-metadata.xml
Downloading: http://repository.springsource.com/maven/bundles/external/org/eclipse/core/resources/maven-metadata.xml
Downloading: http://repository.springsource.com/maven/bundles/release/org/eclipse/core/resources/maven-metadata.xml
Downloading: https://oss.sonatype.org/content/repositories/snapshots/org/eclipse/core/resources/maven-metadata.xml
Why that happens and how I could prevent it?
This usually happens when no version information is specified for the artifact.
maven-metadata.xml is a file which contains the <groupId>, <artifactId> and the versioning information about the various versions available for the dependency.
If the version of the artifact is not specified in the pom.xml, maven downloads this metadata file to check if the local repository contains the latest version.
So, you can avoid this download by specifying the version information of the artifact in pom.xml file, instead of changing the update policy which might affect the update process of other jar files in future.
The most important thing is to start using a repository manager furthermore check the configuration in your settings.xml file which can be configured to check the remote repositories (update policy).
1) Check that you indeed have these artifacts in your local repo
2) Check your repository configuration so you are using your repos only to download releases.
<project>
...
<repositories>
<repository>
<id>my-repo1</id>
<name>your custom repo</name>
<url>http://jarsm2.dyndns.dk</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
...
</project>
3) You can force maven to only use your local repo with the -o option:
mvn -o clean package

Maven fails to get SNAPSHOT builds from repository

Our internal repository (Artifactory) now contains both the stable builds as well as SNAPSHOT versions of our internal libraries.
For stable builds there has never been a problem of downloading anything from the repository.
However, when I add a -SNAPSHOT, Maven claims to be unable to find the dependency, even though it is most definitely in the repository.
If I build and deploy the dependency locally (i.e. into my local repo) all works normally.
Basically, this works:
<dependency>
<groupId>com.example</groupId>
<artifactId>ourlibrary</artifactId>
<version>1.0.0</version>
</dependency>
and this doesn't:
<dependency>
<groupId>com.example</groupId>
<artifactId>ourlibrary</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>
Even though both versions were built the same way and deployed (as far as I can possibly tell) correctly to the repository.
The error:
Missing:
----------
1) com.example:ourlibrary:jar:1.0.1-SNAPSHOT,
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=com.example -DartifactId=ourlibrary -Dversion=1.0.1-SNAPSHOT, -Dpackaging=jar -Dfile=/path/to/file
Alternatively, if you host your own repository you can deploy the file there:
mvn deploy:deploy-file -DgroupId=com.example -DartifactId=ourlibrary -Dversion=1.0.1-SNAPSHOT, -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
Path to dependency:
1) com.example:product:war:2.0.0-SNAPSHOT
2) com.example:ourlibrary:jar:1.0.1-SNAPSHOT,
While this sounds similar to this question, the resolution arrived at there does not apply to my case.
Any insights into this issue would be greatly appreciated.
Edit
Running with -X (as John V. suggested) revealed the following:
[DEBUG] Skipping disabled repository central
[DEBUG] ourlibrary: using locally installed snapshot
[DEBUG] Skipping disabled repository central
[DEBUG] Using mirror: http://repo.example.com/repo (id: repo.example.com)
[DEBUG] Artifact not found - using stub model: Unable to download the artifact from any repository
com.example:ourlibrary:pom:1.0.1-SNAPSHOT
from the specified remote repositories:
repo.example.com (http://repo.example.com/repo)
[DEBUG] Using defaults for missing POM com.example:ourlibrary:pom:1.0.1-SNAPSHOT:compile
[DEBUG] com.example:ourlibrary:jar:1.0.1-SNAPSHOT:compile (selected for compile)
Two thoughts come to mind:
The path structure in your internal
repository for your artifact is
incorrect. I suggest running the
maven command with -X parameter. It
will display the maven's attempt at
downloading the files. Get the line
that has your repository as the url
and try and look for it yourself.
The path should look like
/com/example/ourlibrary/1.0.1/ourlibrary-1.0.1-SNAPSHOT.jar
You didnt include your repository as
a repository in your pom.xml
Typically you have a separate snapshots url from releases url. Just different paths in the same repository, but listed as separate repositories in the pom. The one for snapshots needs to have snapshots enabled, and the one for releases has snapshots disabled:
<repositories>
<repository>
<id>central</id>
<url>
http://<releases-url>
</url>
**<snapshots>
<enabled>false</enabled>
</snapshots>**
</repository>
<repository>
<id>snapshots</id>
<url>
http://<snapshots-url>
</url>
<snapshots>
**<enabled>true</enabled>**
<!-- never, daily, interval:X (where X is in minutes) or always -->
<!--<updatePolicy>daily</updatePolicy> -->
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>

How can I add source code to my dependency libraries in Maven?

For instance, I have included into my dependencies junit-addons : junit-addons. But in the maven repository there isn't any source code. And I know it exists (I have downloaded it). How can I modify the dependencies in order to use libraries from my local project instead from the maven repository (I would omit the junit-addons from the respository and use a local JAR and its source code instead).
Note: I use m2eclipse.
I've solved the problem in a very straight forward way:
I have copied into the folder ${user.home}/.m2/repository/{group-name}/{artifactId}/{version}/ the source file following MAVEN standard: {artifactId}-{version}-sources.jar and it works as a charm! Eclipse checks the local repository and finds the sources.
I don't know if this is the MAVEN way, though.
How can I modify the dependencies in order to use libraries from my local project instead from the maven repository
You can't. If a sources JAR isn't available in the central repository, just put the sources somewhere on your file system in a folder, JAR or zip (you could install:install-file a sources archive in your local repository, following Maven's conventions) and setup Eclipse to use them (right-click on the JAR under Maven Dependencies in the Package Explorer, select Java Source Attachment and setup the Location path).
I use free version of Artifactory repository. I created a jar file {artifactId}-{version}-sources.jar and uploaded to the repository into the same group-id as binary jar file.
Then in my pom I added dependency:
<dependency>
<groupId>mygroupid</groupId>
<artifactId>artifact</artifactId>
<version>1.0</version>
<classifier>source</classifier>
</dependency>
During maven build phase source jar was downloaded to my local repository.
I use netbeans 7.0 and it automatically managed everything for me. For example, right click on method and choosing go toSource correctly brings me to source code in the jar.
You could use the install-file mojo to locally install artifacts into your local maven repository. If you want to share this artifact with others (say your team or another workstation), you could use your own repository manager (e.g. Nexus) and configure it as a mirror for any repository, e.g. central. Nexus will fetch (and cache) artifacts from central. Additionally, you may upload just about any artifact (like junit-addons sources) to your nexus installation.
In order to do configure a mirror, you'll have to edit ${user.home}/.m2/settings.xml
<settings>
<!-- SNIP -->
<mirrors>
<mirror>
<id>nexus-releases</id>
<mirrorOf>*</mirrorOf>
<!-- replace nexus.example.com with the location of your nexus installation -->
<url>http://nexus.example.com/releases</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
<id>nexus</id>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</profile>
</profiles>
</settings>
After you have downloaded it into your local repository, you could make a copy of it. Give it a new artifactId (e.g. libraryName-myVersion) and add dependencies in the pom. Make sure you change the folder names, jar names, pom names and the artifactId itself in the pom. Store everything in your local repository. Now you can use your hacked version of your dependency.
But to be honest, I do not thing this is a good idea to do. But maybe it helps/could not be avoided in your case.

Categories