I have a project where I use Maven as the build tool.
In my pom.xml file I have these dependencies:
<dependency>
<groupId>com.my.group1</groupId>
<artifactId>MyArtifact1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.my.group2</groupId>
<artifactId>MyArtifact2</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.my.group3</groupId>
<artifactId>MyArtifact3</artifactId>
<version>1.3</version>
</dependency>
Within the folder where I have my pom.xml, I also have a subfolder, \external. In this subfolder, I have the above listed artifacts as jars: MyArtifact1-1.0.jar, MyArtifact2-1.1.jar, MyArtifact3-1.3.jar.
The problem is that when I run mvn install, I get this:
[ERROR] Failed to execute goal on project my-project: Could not resolve dependencies for project com.my.group4:sample-project:jar:20161207.3: The following artifacts could not be resolved: com.my.group1:MyArtifact1:jar:1.0, com.my.group2:MyArtifact2:jar:1.1, com.my.group3:MyArtifact3:jar:1.3: Failure to find com.my.group1:MyArtifact1:jar:1.0 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced
So far, I tied running this:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=./external/MyArtifact1-1.0.jar -DgroupId=com.my.group1 -DartifactId=MyArtifact1 -Dversion=1.0 -Dpackaging=jar
I tried the above for all 3 packages, but still it looks like it didn't have any effect.
You are trying to include in the maven compilation .jar files you downloaded in your computer ?
Remember that Maven will try to get your dependencies from the local repository first (a folder in your computer) and from the global repositories later. If your dependencies (.jar) are not in those repositories, you will get an error message.
Here I give you some ideas/solutions:
If the .jar files resulted from other maven projects
If the .jar files are the product of other maven projects you created in your computer. You can install the outputs of that project in your local repository. Once they are installed, maven will find the dependencies locally.
// in the other projects
mvn clean install
If you have many local projects, you may setup your own maven repository to manage all your reusable components. Many companies setup their own repository in their local network. You can use software such as Artifactory
or Nexus Repository Manager. You can even built a repository using your filesystem. Note that, in order to use these repositories, you have to configure them in the pom.xml file of your project.
<project>
...
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://myserver/repo</url>
</repository>
</repositories>
...
</project>
If the .jar files were downloaded from other websites
Sometimes you need .jar files that exist in global maven repositories. You may try first to search in these repositories. You can go to sites such as MVN repository to search or browse the existing packages in the repositories. If you find the dependencies you need, you can obtain too the required dependency to include in the pom.xml.
If the .jar files does not exist in any repository, you can install the file in your local repository. There is a tutorial in the Heroku's devcenter and another one in Mkyong. You must pick a groupId-artifactId-version for the file, install the file and add the dependency to the pom.xml of the project.
Note that, if the .jar was not created using maven, you must provide all the parameters
mvn install:install-file -Dfile=path-to-your-artifact-jar \
-DgroupId=your.groupId \
-DartifactId=your-artifactId \
-Dversion=version \
-Dpackaging=jar \
-DlocalRepositoryPath=path-to-specific-local-repo
If the .jar was generated with Maven, that file will include their own pom.xml with metadata. If you use Maven 2.5+, you do not need to provide the groupId-artifactId and version.
mvn install:install-file -Dfile=path-to-your-artifact-jar \
-DlocalRepositoryPath=path-to-specific-local-repo
There is another option. You can use the system scope with a local systemPath. However, I think this solution requires that people keep the library in the same location for all the developers, or requires to include the .jar file in the project/code repository. Using a repository is a better idea.
<dependency>
<artifactId>..</artifactId>
<groupId>..</groupId>
<scope>system</scope>
<systemPath>${basedir}/lib/dependency.jar</systemPath>
</dependency>
Please check whether these jars present in your local repository.
You can find it's location from your maven setting.xml file , if not mentioned there then it will take the default path i.e. .m2 folder inside c drive.
You need to check first whether these jars are present there or not. If not, then either you need to copy those in proper folder structure or you can install those jars on your local firing below command :
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
The recommended way is:
use a Maven Repository manager such as Nexus
configure Maven to use a Single Repository Group
configure proxy repos to other Maven Repositories as needed and add them to the group you specified in your settings file (see previous step)
if not already available, configure a repo for 3rd party libs in your repository manager and add it to the group you specified in your settings file (see previous step)
if your dependencies are not available from any repo, then add them to your 3rd party repo
remove all JARs from the source code
Related
This question already has answers here:
Add a dependency in Maven
(5 answers)
Closed 3 years ago.
My area of expertise is not Java. I develop in other languages on other platforms.
Right now I'm developing a series of Java servlets for a project. The servlets are to run on a CentOS server running FileNetP8.
I actually got all of the planned items finished and running.
Now when I am trying to add a few more services, the library I came across is available via Maven. I have no idea what Maven is. Read up on it today. Created a test project to try it out.
My development environment is Eclipse Photon on Windows 10.
Now my problem is I can't figure out how to add the Filenet JARs to the project. I can't upload them to some Maven repo. Searching the Web says to add them to the local repo, but I don't understand how to do that to the Local repo in Eclipse's builtin Maven.
I think that the JARs don't need to be packaged within the deployment WAR because the app will be deployed to the Websphere server that runs Filenet so they should be available on it. Should I add them as external JAR references to get the project to compile?
I provide below the following approaches to check based upon your suitability.
Approach-1: Install manually
If you have only 1 jar file, execute the following command by replacing as per your requirements.
mvn install:install-file \
-Dfile=<file path location> \
-DgroupId=<your own custom group id> \
-DartifactId=<your own custom artifact id> \
-Dversion=<some version number> \
-Dpackaging=jar \
-DgeneratePom=true
Once it is done, use the following in your project pom.xml in the dependency section.
<dependency>
<groupId>your own custom group id</groupId>
<artifactId>your own custom artifact id</artifactId>
<version>some version number</version>
</dependency>
The downside of the above approach is, only you can use it as it installs in your .m2 directory. For other developers, they have to follow the same approach. It is not a suggested approach.
Approach-2: Manually add the location of jar file
Add the following dependency in pom.xml
<dependency>
<groupId>some.group.id</groupId>
<artifactId>some.artifat.id</artifactId>
<version>some.version.no</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/yourActualJarFileName.jar</systemPath>
</dependency>
The downside of the above approach is, you have to put all the jar files in a directory and you have to provide the path. All the jar files should be part of your project, it means all the jar file should be put in source code repository. Although it servers the purposes, still it is not a good approach. If you got a newer/higher version of jar file, again you have to put it inside your lib directory. It means you have manage all the old and new versions of jar files.
Best Approach
Maintain Nexus or Artifactory, or any artifactory management system in the organisation and put all the jar files and provide the definition of your custom jar file. It will provide you pom definition. Based upon this, you have to add the dependencies in your project pom.xml. Here you can maintain n number version of your custom jar files.
Seeing as every answer involves maven i'll try to provide a different and somewhat old school approach. You could always import your jars directly into your project by right clicking on it -> properties -> Add Jars. Apply when done and VoilĂ . This is far easier than understanding the complexity of maven.
Dependencies need to be added in pom.xml under dependencies tag. Maven will download the specified jars from maven central repository for the first time, and it will be saved in your local repo.
If the dependencies are not available in central repo and if they have their own repo, you to need specify it in the repositories tag.
<repositories>
<repository>
<id>repo-id</id>
<name>repo-name</name>
<url>http://repourl</url>
</repository>
</repositories>
Any change in the pom.xml, maven will automatically download it.
<dependencies>
<dependency>
<groupId>dependency-id</groupId>
<artifactId>artifactid</artifactId>
<version>1</version>
</dependency>
</dependencies>
If the jars are available in Maven Central repo then all you hvae to do is add dependency under dependencies section in your pom.xml file. Set the scope as required, like you said jars don't needed to be packaged in WAR file as they might be available on server then you can set scope to provided.
<dependency>
<groupId>groud-id</groupId>
<artifactId>artifact-id</artifactId>
<version>version-numbe</version>
<scope>scope</scope>
</dependency>
If jars are not available in central repo and you want to install them on your local repo then below command should help you. More information here.
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
I was not able to download one jar through maven so i have created one local repository and downloaded jar manually and then added to pom by using system scope
<scope>system</scope>
<systemPath>c:\\bdh\gdhs</systemPath>
but i requires the dependencies of the local repository jar. what should i do then? manually adding each and every dependency jars is not a good solution it seems
someone help please..
Do not use system scope.
Install the downloaded file to local repository.
If you have both pom & artifact
mvn install:install-file -Dfile=<path-to-file> -DpomFile=<path-to-pomfile>
To have its dependencies also resolved correctly - so not to add every dependency by hand - you need the pom.
If you do not have the pom
mvn install:install-file -Dfile=<path-to-file> -DgroupId=**group-id** \
-DartifactId=**artifact-id** -Dversion=**version** -Dpackaging=<packaging>
Without pom the dependencies of installed artifact can not be resolved automatically and you need to manually install them (preferably with pom).
Then just add it as dependency:
<dependency>
<groupId>***group-id**</groupId>
<artifactId>**artifact-id**</artifactId>
<version>**version**</version>
</dependency>
Refer Guide to installing 3rd party JARs for more details
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
When I run
mvn compile
I get package com.ibm.icu.util does not exist. So I downloaded the ICU4J jar and installed it into the local repository. I confirmed it's in .m2/repository/com/ibm/icu/icu4j/3.4.4/icu4j-3.4.4.jar. Inside that jar file is the missing class file com/ibm/icu/util/Calendar.class. Then I added the following into the dependencies section of pom.xml:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>3.4.4</version>
</dependency>
But when I run mvn compile again, I get the same error. What am I doing wrong?
You should avoid adding dependencies manually.
If you don't know a groupId and artifactId of the dependency you need, search for it at http://mvnrepository.com/. Usually, groupId matches the package names in the jar file.
For your case, the dependency is already there: http://mvnrepository.com/search?q=com.ibm.icu
So, go to http://mvnrepository.com/artifact/com.ibm.icu/icu4j and get the version of the dependency you need, e.g. 55.1: http://mvnrepository.com/artifact/com.ibm.icu/icu4j/55.1
Grab maven dependency xml and put it to your pom.xml file:
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>55.1</version>
</dependency>
If you didn't find your dependency try to find it in google. Sometimes the dependency may be found in some corporate public repositories, not in a central. In this case you need to add the third-party repository to repositories section of your pom.xml.
If you're unable to find your dependency in the public repository then you have three options:
A. Install jar to internal repository server (e.g. nexus)
B. Put the JAR file in your project sources and declare project maven repository :
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${basedir}/my-repo</url>
</repository>
</repositories>
Important: You should keep the maven repository layout in your local repository.
C. [Bad Practice] Use maven install plugin to install your custom jar to local repository on your machine. But it's a badIt's not recommended.
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=path-to-your-artifact-jar -DpomFile=path-to-pom
D. [Bad Practice] Use system dependency with absolute path to the JAR file, although it's a bad practice and should be avoided.
<dependency>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>X.Y.Z</version>
<scope>system</scope>
<systemPath>${user.home}/jars/my.jar</systemPath>
</dependency>
You should not be manually installing things into the maven repository directory. That directory is maintained by maven itself.
The way dependencies work is that when you run mvn compile or some other goal, it will connect to the maven central repository and download the needed dependencies and their dependencies.
If you manually install a jar file, it may not have it's dependencies. That icu artifact will likely have other things it depends on. Maven will automatically resolve these dependencies.
I would recommend using mvn clean install this will clean the target directory and rebuild everything.
If it fails to download, then you likely need to change the maven configuration. For example, if you are behind a proxy server you need to configure maven with the proxy credentials.
Also, if you manually copied anything into the .m2/repository/ directory you should delete it and let maven put it in there correctly instead.
The beauty of maven is that you don't need to worry about things like downloading jars. It just handles that for you. Let it do it's job.
If you have an internal artifactory like JFrog maybe you should check that the jar is there. Do not download manually to .m2 because it's at least strange. At most you can upload the jar in that artifactory manually.
I am getting an warning message because I manually added a jar file to my local maven repository.
[INFO] Downloading: http://repo.maven.apache.org/maven2/com/netsuite/nsws-2014/1.0/nsws-2014-1.0.pom
[WARNING] The POM for com.netsuite:nsws-2014:jar:1.0 is missing, no dependency information available
How do I properly add a jar file to my local Maven repository using Eclipse m2e on Luna Service (4.4.1)?
Like can I use the builder?
The local Maven repository is effectively a standard Maven repository; as such, it must adhere to a particular format (there are a few Maven-compatible repository formats).
Dropping files into a repository isn't going to cut it, unless you are able to mimic the creation of additional artifacts in accordance to the Maven repository format that you use.
The only foolproof way to do so would be to use the install:install-file goal, as specified here: http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
To do this using m2e, you need to use a small hack. Create a Maven Launch configuration. In the "base directory" field, type in a value that will always resolve to a real directory, such as ${workspace_loc}. In the "Goals", field, type install:install-file, and then add the parameters (see the link in the paragraph below) in the "parameters" table (use the "Add" button to add parameters). Then run this launch configuration.
You should add jar to your local repository like:
mvn install:install-file -DgroupId=... -DartifactId=... -Dversion=... -Dpackaging=jar -Dfile=... -DgeneratePom=true
-DgeneratePom=true genertes POM automatically...
You need to declare <scope>system</scope> like
<dependency>
<groupId>com.netsuite</groupId>
<artifactId>nsws-2014</artifactId>
<version>1</version>
<scope>system</scope>
<systemPath>/path/to/your.jar</systemPath>
</dependency>