package does not exist when building WAR [duplicate] - maven

How do I add local jar files (not yet part of the Maven repository) directly in my project's library sources?

You can add local dependencies directly (as mentioned in build maven project with propriatery libraries included) like this:
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
</dependency>
Update
In new releases this feature is marked as deprecated but still working and not removed yet ( You just see warning in the log during maven start). An issue is raised at maven group about this https://issues.apache.org/jira/browse/MNG-6523 ( You can participate and describe why this feature is helpful in some cases). I hope this feature remains there!
If you are asking me, as long as the feature is not removed, I use this to make dependency to only one naughty jar file in my project which is not fit in repository. If this feature is removed, well, there are lots of good answers here which I can chose from later!

Install the JAR into your local Maven repository (typically .m2 in your home folder) as follows:
mvn install:install-file \
-Dfile=<path-to-file> \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<packaging> \
-DgeneratePom=true
Where each refers to:
<path-to-file>: the path to the file to load e.g → c:\kaptcha-2.3.jar
<group-id>: the group that the file should be registered under e.g → com.google.code
<artifact-id>: the artifact name for the file e.g → kaptcha
<version>: the version of the file e.g → 2.3
<packaging>: the packaging of the file e.g. → jar
Reference
Maven FAQ: I have a jar that I want to put into my local repository. How can I copy it in?
Maven Install Plugin Usage: The install:install-file goal

Firstly, I would like to give credit for this answer to an anonymous Stack Overflow user - I am pretty sure I've seen a similar answer here before - but now I cannot find it.
The best option for having local JAR files as a dependency is to create a local Maven repository. Such a repository is nothing more than a proper directory structure with pom files in it.
For my example:
I have my master project on ${master_project} location and subproject1 is on ${master_project}/${subproject1}.
Then I create a Maven repository in:
${master_project}/local-maven-repo.
In the pom file in subproject1 located at ${master_project}/${subproject1}/pom.xml, the repository needs to be specified which would take file path as a URL parameter:
<repositories>
<repository>
<id>local-maven-repo</id>
<url>file:///${project.parent.basedir}/local-maven-repo</url>
</repository>
</repositories>
The dependency can be specified as for any other repository. This makes your pom repository independent. For instance, once the desired JAR is available in Maven central, you just need to delete it from your local repo and it will be pulled from the default repo.
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.servicebinder</artifactId>
<version>0.9.0-SNAPSHOT</version>
</dependency>
The last but not least thing to do is to add the JAR file to local repository using -DlocalRepositoryPath switch like so:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \
-Dfile=/some/path/on/my/local/filesystem/felix/servicebinder/target/org.apache.felix.servicebinder-0.9.0-SNAPSHOT.jar \
-DgroupId=org.apache.felix -DartifactId=org.apache.felix.servicebinder \
-Dversion=0.9.0-SNAPSHOT -Dpackaging=jar \
-DlocalRepositoryPath=${master_project}/local-maven-repo
Once the JAR file is installed, your Maven repo can be committed to a code repository, and the whole set-up is system independent. (Working example in GitHub).
I agree that having JARs committed to source code repo is not a good practice, but in real life, quick and dirty solutions are sometimes better than a full blown Nexus repo to host one JAR that you cannot publish.

Create a new folder, let's say local-maven-repo at the root of your Maven project.
Just add a local repo inside your <project> of your pom.xml:
<repositories>
<repository>
<id>local-maven-repo</id>
<url>file:///${project.basedir}/local-maven-repo</url>
</repository>
</repositories>
Then for each external jar you want to install, go at the root of your project and execute:
mvn deploy:deploy-file -DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=[FILE_PATH]

I'd like such solution - use maven-install-plugin in pom file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>lib/yourJar.jar</file>
<groupId>com.somegroup.id</groupId>
<artifactId>artefact-id</artifactId>
<version>x.y.z</version>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
In this case you can perform mvn initialize and jar will be installed in local maven repo. Now this jar is available during any maven step on this machine (do not forget to include this dependency as any other maven dependency in pom with <dependency></dependency> tag). It is also possible to bind jar install not to initialize step, but any other step you like.

The really quick and dirty way is to point to a local file, please note "system" is deprecated by now:
<dependency>
<groupId>com.sample</groupId>
<artifactId>samplifact</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:\DEV\myfunnylib\yourJar.jar</systemPath>
</dependency>
However this will only live on your machine (obviously), for sharing it usually makes sense to use a proper m2 archive (nexus/artifactory) or if you do not have any of these or don't want to set one up a local maven structured archive and configure a "repository" in your pom:
local:
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://C:/DEV//mymvnrepo</url>
</repository>
</repositories>
remote:
<repositories>
<repository>
<id>my-remote-repo</id>
<url>http://192.168.0.1/whatever/mavenserver/youwant/repo</url>
</repository>
</repositories>
for this solution, a relative path is also possible using the basedir variable:
<url>file:${basedir}</url>

<dependency>
<groupId>group id name</groupId>
<artifactId>artifact name</artifactId>
<version>version number</version>
<scope>system</scope>
<systemPath>jar location</systemPath>
</dependency>

Important part in dependency is:
${pom.basedir} (instead of just ${basedir})
<dependency>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/lib/example.jar</systemPath>
</dependency>

Add your own local JAR in POM file and use that in maven build.
mvn install:install-file -Dfile=path-to-jar -DgroupId=owngroupid -DartifactId=ownartifactid -Dversion=ownversion -Dpackaging=jar
For example:
mvn install:install-file -Dfile=path-to-jar -DgroupId=com.decompiler -DartifactId=jd-core-java -Dversion=1.2 -Dpackaging=jar
Then add it to the POM like this:

Step 1: Configure the maven-install-plugin with the goal install-file in your pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-external-non-maven-jar-MWS-Client-into-local-maven-repo</id>
<phase>clean</phase>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>com.amazonservices.mws</groupId>
<artifactId>mws-client</artifactId>
<version>1.0</version>
<file>${project.basedir}/lib/MWSClientJavaRuntime-1.0.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
Make sure to edit the file path based on your actual file path (recommended is to place these external non-maven jars inside some folder, let's say lib, and place this lib folder inside your project so as to use project-specific relative path and avoid adding system specific absolute path.
If you have multiple external jars, just repeat the <execution> for other jars within the same maven-install-plugin.
Step 2: Once you have configured the maven-install-plugin as shown above in your pom.xml file, you have to use these jars in your pom.xml as usual:
<dependency>
<groupId>com.amazonservices.mws</groupId>
<artifactId>mws-client</artifactId>
<version>1.0</version>
</dependency>
Note that the maven-install-plugin only copies your external jars to your local .m2 maven repository. That's it. It doesn't automatically include these jars as maven dependencies to your project.
It's a minor point, but sometimes easy to miss.

One way is to upload it to your own Maven repository manager (such as Nexus). It's good practice to have an own repository manager anyway.
Another nice way I've recently seen is to include the Maven Install Plugin in your build lifecycle: You declare in the POM to install the files to the local repository. It's a little but small overhead and no manual step involved.
http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html

Of course you can add jars to that folder. But maybe it does not what you want to achieve...
If you need these jars for compilation, check this related question: Can I add jars to maven 2 build classpath without installing them?
Also, before anyone suggests it, do NOT use the system scope.

Another interesting case is when you want to have in your project private maven jars. You may want to keep the capabilities of Maven to resolve transitive dependencies. The solution is fairly easy.
Create a folder libs in your project
Add the following lines in your pom.xml file
<properties><local.repository.folder>${pom.basedir}/libs/</local.repository.folder>
</properties>
<repositories>
<repository>
<id>local-maven-repository</id>
<url>file://${local.repository.folder}</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
Open the .m2/repository folder and copy the directory structure of the project you want to import into the libs folder.
E.g. suppose you want to import the dependency
<dependency>
<groupId>com.mycompany.myproject</groupId>
<artifactId>myproject</artifactId>
<version>1.2.3</version>
</dependency>
Just go on .m2/repository and you will see the following folder
com/mycompany/myproject/1.2.3
Copy everything in your libs folder (again, including the folders under .m2/repository) and you are done.

Add local jar libraries, their sources and javadoc to a Maven project
If you have pre-compiled jar files with libraries, their sources and javadoc, then you can install them to your local Maven repository like this:
mvn install:install-file
-Dfile=awesomeapp-1.0.1.jar \
-DpomFile=awesomeapp-1.0.1.pom \
-Dsources=awesomeapp-1.0.1-sources.jar \
-Djavadoc=awesomeapp-1.0.1-javadoc.jar \
-DgroupId=com.example \
-DartifactId=awesomeapp \
-Dversion=1.0.1 \
-Dpackaging=jar
Then in your project you can use this libraries:
<!-- com.example -->
<dependency>
<groupId>com.example</groupId>
<artifactId>awesomeapp</artifactId>
<version>1.0.1</version>
</dependency>
See: maven-install-plugin usage.
Or you can build these libraries yourself with their sources and javadoc using maven-source-plugin and maven-javadoc-plugin, and then install them.
Example project: library
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<url>https://example.com/awesomeapp</url>
<groupId>com.example</groupId>
<artifactId>awesomeapp</artifactId>
<name>awesomeapp</name>
<version>1.0.1</version>
<packaging>jar</packaging>
<properties>
<java.version>12</java.version>
</properties>
<build>
<finalName>awesomeapp</finalName>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals><goal>jar</goal></goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals><goal>jar</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Execute maven install goal:
mvn install
Check your local Maven repository:
~/.m2/repository/com/example/awesomeapp/1.0.1/
├─ _remote.repositories
├─ awesomeapp-1.0.1.jar
├─ awesomeapp-1.0.1.pom
├─ awesomeapp-1.0.1-javadoc.jar
└─ awesomeapp-1.0.1-sources.jar
Then you can use this library:
<!-- com.example -->
<dependency>
<groupId>com.example</groupId>
<artifactId>awesomeapp</artifactId>
<version>1.0.1</version>
</dependency>

command line :
mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar

I think a better solution for this problem is to use maven-install-plugin to automatically install the files at install time. This is how I set it up for my project.
First, add the path (where you store the local .jars) as a property.
<properties>
<local.sdk>/path/to/jar</local.sdk>
</properties>
Then, under plugins add a plugin to install the jars when compiling.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>1</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${local.sdk}/lib/impl/appengine-api.jar</file>
</configuration>
</execution>
<execution>
<id>appengine-api-stubs</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${local.sdk}/lib/impl/appengine-api-stubs.jar</file>
</configuration>
</execution>
</executions>
</plugin>
Finally, in dependencies, you can add the jars
<dependency>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
By Setting up your project like this, the project will continue to build even when you take it to another computer (given that it has all the jar files in the path specified by the property local.sdk).
For groupId use a unique name just to make sure that there are no conflicts.
Now when you mvn install or mvn test local jars will be added automatically.

Not an answer to the original question, however it might be useful for someone
There is no proper way to add multiple jar libraries from the folder using Maven. If there are only few dependencies, it is probably easier to configure maven-install-plugin as mentioned in the answers above.
However for my particular case, I had a lib folder with more than 100 proprietary jar files which I had to add somehow. And for me it was much easier for me to convert my Maven project to Gradle.
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
flatDir {
dirs 'libs' // local libs folder
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'io.grpc:grpc-netty-shaded:1.29.0'
implementation 'io.grpc:grpc-protobuf:1.29.0'
implementation 'io.grpc:grpc-stub:1.29.0' // dependecies from maven central
implementation name: 'akka-actor_2.12-2.6.1' // dependecies from lib folder
implementation name: 'akka-protobuf-v3_2.12-2.6.1'
implementation name: 'akka-stream_2.12-2.6.1'
}

This is a short syntax for newer versions:
mvn install:install-file -Dfile=<path-to-file>
It works when the JAR was built by Apache Maven - the most common case. Then it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default.
Source: http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

The preferred way would be to create your own remote repository.
See here for details on how to do it.
Have a look at the 'Uploading to a Remote Repository' section.

I want to share a code where you can upload a folder full of jars. It's useful when a provider doesn't have a public repository and you need to add lots of libraries manually. I've decided to build a .bat instead of call directly to maven because It could be Out of Memory errors. It was prepared for a windows environment but is easy to adapt it to linux OS:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
public class CreateMavenRepoApp {
private static final String OCB_PLUGIN_FOLDER = "C://your_folder_with_jars";
public static void main(String[] args) throws IOException {
File directory = new File();
//get all the files from a directory
PrintWriter writer = new PrintWriter("update_repo_maven.bat", "UTF-8");
writer.println("rem "+ new Date());
File[] fList = directory.listFiles();
for (File file : fList){
if (file.isFile()){
String absolutePath = file.getAbsolutePath() ;
Manifest m = new JarFile(absolutePath).getManifest();
Attributes attributes = m.getMainAttributes();
String symbolicName = attributes.getValue("Bundle-SymbolicName");
if(symbolicName!=null &&symbolicName.contains("com.yourCompany.yourProject")) {
String[] parts =symbolicName.split("\\.");
String artifactId = parts[parts.length-1];
String groupId = symbolicName.substring(0,symbolicName.length()-artifactId.length()-1);
String version = attributes.getValue("Bundle-Version");
String mavenLine= "call mvn org.apache.maven.plugins:maven-install-plugin:2.5.1:install-file -Dfile="+ absolutePath+" -DgroupId="+ groupId+" -DartifactId="+ artifactId+" -Dversion="+ version+" -Dpackaging=jar ";
writer.println(mavenLine);
}
}
}
writer.close();
}
}
After run this main from any IDE, run the update_repo_maven.bat.

Also take a look at...
<scope>compile</scope>
Maven Dependencies. This is the default but I've found in some cases explicitly setting that scope also Maven to find local libraries in the local repository.

Create a local Maven repository directory, Your project root should look something like this to start with:
yourproject
+- pom.xml
+- src
Add a standard Maven repository directory called repo for the group com.example and version 1.0:
yourproject
+- pom.xml
+- src
+- repo
Deploy the Artifact Into the Repo, Maven can deploy the artifact for you using the mvn deploy:deploy-file goal:
mvn deploy:deploy-file -Durl=file:///pathtoyour/repo -Dfile=your.jar -DgroupId=your.group.id -DartifactId=yourid -Dpackaging=jar -Dversion=1.0
install pom file corresponding to your jar so that your project can find jar during maven build from local repo:
mvn install:install-file -Dfile=/path-to-your-jar-1.0.jar -DpomFile=/path-to-your-pom-1.0.pom
add repo in your pom file:
<repositories>
<!--other repositories if any-->
<repository>
<id>project.local</id>
<name>project</name>
<url>file:${project.basedir}/repo</url>
</repository>
</repositories>
add the dependency in your pom:
<dependency>
<groupId>com.groupid</groupId>
<artifactId>myid</artifactId>
<version>1.0</version>
</dependency>

To install third party jar, Please call the command like below
mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path

For some reason, in the web application I'm giving maintenance to, neither Alireza Fattahi's solution nor JJ Roman's solution worked correctly. In both cases, the compilation goes okay (it sees the jar), but the packaging fails to include the jar inside the war.
The only way I managed to make it work was by putting the jar on /src/main/webapp/WEB-INF/lib/ and then combining it with either Fattahis's or Roman's solution.

Note that it is NOT necessarily a good idea to use a local repo.
If this project is shared with others then everyone else will have problems and questions when it doesn't work, and the jar won't be available even in your source control system!
Although the shared repo is the best answer, if you cannot do this for some reason then embedding the jar is better than a local repo. Local-only repo contents can cause lots of problems, especially over time.

On your local repository you can install your jar by issuing the commands
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
Follow this useful link to do the same from mkyoung's website. You can also check maven guide for the same

mvn install
You can write code below in command line or if you're using eclipse builtin maven right click on project -> Run As -> run configurations... -> in left panel right click on Maven Build -> new configuration -> write the code in Goals & in base directory :${project_loc:NameOfYourProject} -> Run
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where each refers to:
< path-to-file >: the path to the file to load e.g -> c:\kaptcha-2.3.jar
< group-id >: the group that the file should be registered under e.g -> com.google.code
< artifact-id >: the artifact name for the file e.g -> kaptcha
< version >: the version of the file e.g -> 2.3
< packaging >: the packaging of the file e.g. -> jar
2.After installed, just declares jar in pom.xml.
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3</version>
</dependency>

Perhaps someone will be interested in:
https://github.com/Limraj/maven-artifact-generator
Console program to generate maven artifacts in the local repository, and configure dependencies for pom.xml, based on the path to the jars.
You can do this for one file, but it's most useful if you have multiple jar files.
path jars:
java -jar maven-artifact-generator-X.X.X.jar -p path_to_jars -g com.test -V 1.2.3 -P jar
jar:
java -jar maven-artifact-generator-X.X.X.jar -f file_jar -g com.test -V 1.2.3 -P jar
This will generate an artifact in the local maven repository, and generate dependecies for pom.xml in gen.log. ArtifactId is the name of the jar file.
Requires an installed maven.
Testing on widnows 7 and macOS X (unix/linux).

Download jar file
copy jar file to the project folder
get inteliJ idea Maven command area
type below command
mvn install:install-file -Dfile=YOUR_JAR_FILE_LOCATION*JARNAME.jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=jar*
example :
mvn install:install-file
-Dfile=C:\Users\ranushka.l\Desktop\test\spring-web-1.0.2.jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=jar

I had the same error for a set of dependencies in my pom.xml turns out the versions of the dependencies was not specified in the pom.xml and was mentioned in the parent repository. For some reason the version details was not syncing with this repo. Hence i manually entered the versions using the tag and it worked like a charm. Little bit of time needed to look up the versions in the parent and specify here. But this can be done just for the jars that are showing the artifactid error and it works. Hope this helps someone.

Related

How can I install maven dependencies from local? [duplicate]

How do I add local jar files (not yet part of the Maven repository) directly in my project's library sources?
You can add local dependencies directly (as mentioned in build maven project with propriatery libraries included) like this:
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/Name_Your_JAR.jar</systemPath>
</dependency>
Update
In new releases this feature is marked as deprecated but still working and not removed yet ( You just see warning in the log during maven start). An issue is raised at maven group about this https://issues.apache.org/jira/browse/MNG-6523 ( You can participate and describe why this feature is helpful in some cases). I hope this feature remains there!
If you are asking me, as long as the feature is not removed, I use this to make dependency to only one naughty jar file in my project which is not fit in repository. If this feature is removed, well, there are lots of good answers here which I can chose from later!
Install the JAR into your local Maven repository (typically .m2 in your home folder) as follows:
mvn install:install-file \
-Dfile=<path-to-file> \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=<packaging> \
-DgeneratePom=true
Where each refers to:
<path-to-file>: the path to the file to load e.g → c:\kaptcha-2.3.jar
<group-id>: the group that the file should be registered under e.g → com.google.code
<artifact-id>: the artifact name for the file e.g → kaptcha
<version>: the version of the file e.g → 2.3
<packaging>: the packaging of the file e.g. → jar
Reference
Maven FAQ: I have a jar that I want to put into my local repository. How can I copy it in?
Maven Install Plugin Usage: The install:install-file goal
Firstly, I would like to give credit for this answer to an anonymous Stack Overflow user - I am pretty sure I've seen a similar answer here before - but now I cannot find it.
The best option for having local JAR files as a dependency is to create a local Maven repository. Such a repository is nothing more than a proper directory structure with pom files in it.
For my example:
I have my master project on ${master_project} location and subproject1 is on ${master_project}/${subproject1}.
Then I create a Maven repository in:
${master_project}/local-maven-repo.
In the pom file in subproject1 located at ${master_project}/${subproject1}/pom.xml, the repository needs to be specified which would take file path as a URL parameter:
<repositories>
<repository>
<id>local-maven-repo</id>
<url>file:///${project.parent.basedir}/local-maven-repo</url>
</repository>
</repositories>
The dependency can be specified as for any other repository. This makes your pom repository independent. For instance, once the desired JAR is available in Maven central, you just need to delete it from your local repo and it will be pulled from the default repo.
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.servicebinder</artifactId>
<version>0.9.0-SNAPSHOT</version>
</dependency>
The last but not least thing to do is to add the JAR file to local repository using -DlocalRepositoryPath switch like so:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \
-Dfile=/some/path/on/my/local/filesystem/felix/servicebinder/target/org.apache.felix.servicebinder-0.9.0-SNAPSHOT.jar \
-DgroupId=org.apache.felix -DartifactId=org.apache.felix.servicebinder \
-Dversion=0.9.0-SNAPSHOT -Dpackaging=jar \
-DlocalRepositoryPath=${master_project}/local-maven-repo
Once the JAR file is installed, your Maven repo can be committed to a code repository, and the whole set-up is system independent. (Working example in GitHub).
I agree that having JARs committed to source code repo is not a good practice, but in real life, quick and dirty solutions are sometimes better than a full blown Nexus repo to host one JAR that you cannot publish.
Create a new folder, let's say local-maven-repo at the root of your Maven project.
Just add a local repo inside your <project> of your pom.xml:
<repositories>
<repository>
<id>local-maven-repo</id>
<url>file:///${project.basedir}/local-maven-repo</url>
</repository>
</repositories>
Then for each external jar you want to install, go at the root of your project and execute:
mvn deploy:deploy-file -DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] -Durl=file:./local-maven-repo/ -DrepositoryId=local-maven-repo -DupdateReleaseInfo=true -Dfile=[FILE_PATH]
I'd like such solution - use maven-install-plugin in pom file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>lib/yourJar.jar</file>
<groupId>com.somegroup.id</groupId>
<artifactId>artefact-id</artifactId>
<version>x.y.z</version>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
In this case you can perform mvn initialize and jar will be installed in local maven repo. Now this jar is available during any maven step on this machine (do not forget to include this dependency as any other maven dependency in pom with <dependency></dependency> tag). It is also possible to bind jar install not to initialize step, but any other step you like.
The really quick and dirty way is to point to a local file, please note "system" is deprecated by now:
<dependency>
<groupId>com.sample</groupId>
<artifactId>samplifact</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:\DEV\myfunnylib\yourJar.jar</systemPath>
</dependency>
However this will only live on your machine (obviously), for sharing it usually makes sense to use a proper m2 archive (nexus/artifactory) or if you do not have any of these or don't want to set one up a local maven structured archive and configure a "repository" in your pom:
local:
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://C:/DEV//mymvnrepo</url>
</repository>
</repositories>
remote:
<repositories>
<repository>
<id>my-remote-repo</id>
<url>http://192.168.0.1/whatever/mavenserver/youwant/repo</url>
</repository>
</repositories>
for this solution, a relative path is also possible using the basedir variable:
<url>file:${basedir}</url>
<dependency>
<groupId>group id name</groupId>
<artifactId>artifact name</artifactId>
<version>version number</version>
<scope>system</scope>
<systemPath>jar location</systemPath>
</dependency>
Important part in dependency is:
${pom.basedir} (instead of just ${basedir})
<dependency>
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/lib/example.jar</systemPath>
</dependency>
Add your own local JAR in POM file and use that in maven build.
mvn install:install-file -Dfile=path-to-jar -DgroupId=owngroupid -DartifactId=ownartifactid -Dversion=ownversion -Dpackaging=jar
For example:
mvn install:install-file -Dfile=path-to-jar -DgroupId=com.decompiler -DartifactId=jd-core-java -Dversion=1.2 -Dpackaging=jar
Then add it to the POM like this:
Step 1: Configure the maven-install-plugin with the goal install-file in your pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-external-non-maven-jar-MWS-Client-into-local-maven-repo</id>
<phase>clean</phase>
<configuration>
<repositoryLayout>default</repositoryLayout>
<groupId>com.amazonservices.mws</groupId>
<artifactId>mws-client</artifactId>
<version>1.0</version>
<file>${project.basedir}/lib/MWSClientJavaRuntime-1.0.jar</file>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
Make sure to edit the file path based on your actual file path (recommended is to place these external non-maven jars inside some folder, let's say lib, and place this lib folder inside your project so as to use project-specific relative path and avoid adding system specific absolute path.
If you have multiple external jars, just repeat the <execution> for other jars within the same maven-install-plugin.
Step 2: Once you have configured the maven-install-plugin as shown above in your pom.xml file, you have to use these jars in your pom.xml as usual:
<dependency>
<groupId>com.amazonservices.mws</groupId>
<artifactId>mws-client</artifactId>
<version>1.0</version>
</dependency>
Note that the maven-install-plugin only copies your external jars to your local .m2 maven repository. That's it. It doesn't automatically include these jars as maven dependencies to your project.
It's a minor point, but sometimes easy to miss.
One way is to upload it to your own Maven repository manager (such as Nexus). It's good practice to have an own repository manager anyway.
Another nice way I've recently seen is to include the Maven Install Plugin in your build lifecycle: You declare in the POM to install the files to the local repository. It's a little but small overhead and no manual step involved.
http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html
Of course you can add jars to that folder. But maybe it does not what you want to achieve...
If you need these jars for compilation, check this related question: Can I add jars to maven 2 build classpath without installing them?
Also, before anyone suggests it, do NOT use the system scope.
Another interesting case is when you want to have in your project private maven jars. You may want to keep the capabilities of Maven to resolve transitive dependencies. The solution is fairly easy.
Create a folder libs in your project
Add the following lines in your pom.xml file
<properties><local.repository.folder>${pom.basedir}/libs/</local.repository.folder>
</properties>
<repositories>
<repository>
<id>local-maven-repository</id>
<url>file://${local.repository.folder}</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
Open the .m2/repository folder and copy the directory structure of the project you want to import into the libs folder.
E.g. suppose you want to import the dependency
<dependency>
<groupId>com.mycompany.myproject</groupId>
<artifactId>myproject</artifactId>
<version>1.2.3</version>
</dependency>
Just go on .m2/repository and you will see the following folder
com/mycompany/myproject/1.2.3
Copy everything in your libs folder (again, including the folders under .m2/repository) and you are done.
Add local jar libraries, their sources and javadoc to a Maven project
If you have pre-compiled jar files with libraries, their sources and javadoc, then you can install them to your local Maven repository like this:
mvn install:install-file
-Dfile=awesomeapp-1.0.1.jar \
-DpomFile=awesomeapp-1.0.1.pom \
-Dsources=awesomeapp-1.0.1-sources.jar \
-Djavadoc=awesomeapp-1.0.1-javadoc.jar \
-DgroupId=com.example \
-DartifactId=awesomeapp \
-Dversion=1.0.1 \
-Dpackaging=jar
Then in your project you can use this libraries:
<!-- com.example -->
<dependency>
<groupId>com.example</groupId>
<artifactId>awesomeapp</artifactId>
<version>1.0.1</version>
</dependency>
See: maven-install-plugin usage.
Or you can build these libraries yourself with their sources and javadoc using maven-source-plugin and maven-javadoc-plugin, and then install them.
Example project: library
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<url>https://example.com/awesomeapp</url>
<groupId>com.example</groupId>
<artifactId>awesomeapp</artifactId>
<name>awesomeapp</name>
<version>1.0.1</version>
<packaging>jar</packaging>
<properties>
<java.version>12</java.version>
</properties>
<build>
<finalName>awesomeapp</finalName>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals><goal>jar</goal></goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals><goal>jar</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Execute maven install goal:
mvn install
Check your local Maven repository:
~/.m2/repository/com/example/awesomeapp/1.0.1/
├─ _remote.repositories
├─ awesomeapp-1.0.1.jar
├─ awesomeapp-1.0.1.pom
├─ awesomeapp-1.0.1-javadoc.jar
└─ awesomeapp-1.0.1-sources.jar
Then you can use this library:
<!-- com.example -->
<dependency>
<groupId>com.example</groupId>
<artifactId>awesomeapp</artifactId>
<version>1.0.1</version>
</dependency>
command line :
mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar
I think a better solution for this problem is to use maven-install-plugin to automatically install the files at install time. This is how I set it up for my project.
First, add the path (where you store the local .jars) as a property.
<properties>
<local.sdk>/path/to/jar</local.sdk>
</properties>
Then, under plugins add a plugin to install the jars when compiling.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>1</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${local.sdk}/lib/impl/appengine-api.jar</file>
</configuration>
</execution>
<execution>
<id>appengine-api-stubs</id>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${local.sdk}/lib/impl/appengine-api-stubs.jar</file>
</configuration>
</execution>
</executions>
</plugin>
Finally, in dependencies, you can add the jars
<dependency>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.local.jar</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
By Setting up your project like this, the project will continue to build even when you take it to another computer (given that it has all the jar files in the path specified by the property local.sdk).
For groupId use a unique name just to make sure that there are no conflicts.
Now when you mvn install or mvn test local jars will be added automatically.
Not an answer to the original question, however it might be useful for someone
There is no proper way to add multiple jar libraries from the folder using Maven. If there are only few dependencies, it is probably easier to configure maven-install-plugin as mentioned in the answers above.
However for my particular case, I had a lib folder with more than 100 proprietary jar files which I had to add somehow. And for me it was much easier for me to convert my Maven project to Gradle.
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
flatDir {
dirs 'libs' // local libs folder
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'io.grpc:grpc-netty-shaded:1.29.0'
implementation 'io.grpc:grpc-protobuf:1.29.0'
implementation 'io.grpc:grpc-stub:1.29.0' // dependecies from maven central
implementation name: 'akka-actor_2.12-2.6.1' // dependecies from lib folder
implementation name: 'akka-protobuf-v3_2.12-2.6.1'
implementation name: 'akka-stream_2.12-2.6.1'
}
This is a short syntax for newer versions:
mvn install:install-file -Dfile=<path-to-file>
It works when the JAR was built by Apache Maven - the most common case. Then it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default.
Source: http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
The preferred way would be to create your own remote repository.
See here for details on how to do it.
Have a look at the 'Uploading to a Remote Repository' section.
I want to share a code where you can upload a folder full of jars. It's useful when a provider doesn't have a public repository and you need to add lots of libraries manually. I've decided to build a .bat instead of call directly to maven because It could be Out of Memory errors. It was prepared for a windows environment but is easy to adapt it to linux OS:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
public class CreateMavenRepoApp {
private static final String OCB_PLUGIN_FOLDER = "C://your_folder_with_jars";
public static void main(String[] args) throws IOException {
File directory = new File();
//get all the files from a directory
PrintWriter writer = new PrintWriter("update_repo_maven.bat", "UTF-8");
writer.println("rem "+ new Date());
File[] fList = directory.listFiles();
for (File file : fList){
if (file.isFile()){
String absolutePath = file.getAbsolutePath() ;
Manifest m = new JarFile(absolutePath).getManifest();
Attributes attributes = m.getMainAttributes();
String symbolicName = attributes.getValue("Bundle-SymbolicName");
if(symbolicName!=null &&symbolicName.contains("com.yourCompany.yourProject")) {
String[] parts =symbolicName.split("\\.");
String artifactId = parts[parts.length-1];
String groupId = symbolicName.substring(0,symbolicName.length()-artifactId.length()-1);
String version = attributes.getValue("Bundle-Version");
String mavenLine= "call mvn org.apache.maven.plugins:maven-install-plugin:2.5.1:install-file -Dfile="+ absolutePath+" -DgroupId="+ groupId+" -DartifactId="+ artifactId+" -Dversion="+ version+" -Dpackaging=jar ";
writer.println(mavenLine);
}
}
}
writer.close();
}
}
After run this main from any IDE, run the update_repo_maven.bat.
Also take a look at...
<scope>compile</scope>
Maven Dependencies. This is the default but I've found in some cases explicitly setting that scope also Maven to find local libraries in the local repository.
Create a local Maven repository directory, Your project root should look something like this to start with:
yourproject
+- pom.xml
+- src
Add a standard Maven repository directory called repo for the group com.example and version 1.0:
yourproject
+- pom.xml
+- src
+- repo
Deploy the Artifact Into the Repo, Maven can deploy the artifact for you using the mvn deploy:deploy-file goal:
mvn deploy:deploy-file -Durl=file:///pathtoyour/repo -Dfile=your.jar -DgroupId=your.group.id -DartifactId=yourid -Dpackaging=jar -Dversion=1.0
install pom file corresponding to your jar so that your project can find jar during maven build from local repo:
mvn install:install-file -Dfile=/path-to-your-jar-1.0.jar -DpomFile=/path-to-your-pom-1.0.pom
add repo in your pom file:
<repositories>
<!--other repositories if any-->
<repository>
<id>project.local</id>
<name>project</name>
<url>file:${project.basedir}/repo</url>
</repository>
</repositories>
add the dependency in your pom:
<dependency>
<groupId>com.groupid</groupId>
<artifactId>myid</artifactId>
<version>1.0</version>
</dependency>
To install third party jar, Please call the command like below
mvn install:install-file -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar -Dfile=path
For some reason, in the web application I'm giving maintenance to, neither Alireza Fattahi's solution nor JJ Roman's solution worked correctly. In both cases, the compilation goes okay (it sees the jar), but the packaging fails to include the jar inside the war.
The only way I managed to make it work was by putting the jar on /src/main/webapp/WEB-INF/lib/ and then combining it with either Fattahis's or Roman's solution.
Note that it is NOT necessarily a good idea to use a local repo.
If this project is shared with others then everyone else will have problems and questions when it doesn't work, and the jar won't be available even in your source control system!
Although the shared repo is the best answer, if you cannot do this for some reason then embedding the jar is better than a local repo. Local-only repo contents can cause lots of problems, especially over time.
On your local repository you can install your jar by issuing the commands
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
Follow this useful link to do the same from mkyoung's website. You can also check maven guide for the same
mvn install
You can write code below in command line or if you're using eclipse builtin maven right click on project -> Run As -> run configurations... -> in left panel right click on Maven Build -> new configuration -> write the code in Goals & in base directory :${project_loc:NameOfYourProject} -> Run
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where each refers to:
< path-to-file >: the path to the file to load e.g -> c:\kaptcha-2.3.jar
< group-id >: the group that the file should be registered under e.g -> com.google.code
< artifact-id >: the artifact name for the file e.g -> kaptcha
< version >: the version of the file e.g -> 2.3
< packaging >: the packaging of the file e.g. -> jar
2.After installed, just declares jar in pom.xml.
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3</version>
</dependency>
Perhaps someone will be interested in:
https://github.com/Limraj/maven-artifact-generator
Console program to generate maven artifacts in the local repository, and configure dependencies for pom.xml, based on the path to the jars.
You can do this for one file, but it's most useful if you have multiple jar files.
path jars:
java -jar maven-artifact-generator-X.X.X.jar -p path_to_jars -g com.test -V 1.2.3 -P jar
jar:
java -jar maven-artifact-generator-X.X.X.jar -f file_jar -g com.test -V 1.2.3 -P jar
This will generate an artifact in the local maven repository, and generate dependecies for pom.xml in gen.log. ArtifactId is the name of the jar file.
Requires an installed maven.
Testing on widnows 7 and macOS X (unix/linux).
Download jar file
copy jar file to the project folder
get inteliJ idea Maven command area
type below command
mvn install:install-file -Dfile=YOUR_JAR_FILE_LOCATION*JARNAME.jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=jar*
example :
mvn install:install-file
-Dfile=C:\Users\ranushka.l\Desktop\test\spring-web-1.0.2.jar -DgroupId=org.primefaces.themes -DartifactId=iMetro -Dversion=1.0.1 -Dpackaging=jar
I had the same error for a set of dependencies in my pom.xml turns out the versions of the dependencies was not specified in the pom.xml and was mentioned in the parent repository. For some reason the version details was not syncing with this repo. Hence i manually entered the versions using the tag and it worked like a charm. Little bit of time needed to look up the versions in the parent and specify here. But this can be done just for the jars that are showing the artifactid error and it works. Hope this helps someone.

how to add jar dependency to xtext maven build

What is the correct way to use a maven jar file in my xtext dsl project?
What I have tried is this:
use the maven-dependency-plugin in the pom.xml file of the *.dsl project to download the .jar file from a maven repository into the ./lib/ directory. This is done as early as possible in the build process: in the maven validate phase
in MANIFEST.MF: add the jar to the classpath: e.g. Bundle-ClassPath: ., lib/value-2.5.6-annotations.jar
in build.properties: add it to the bin.includes
The problem is, that the build only works when I call mvn install twice.
The first time, the .jar file is downloaded to the lib directory as expected (early in the build process), but then the build fails because it cannot resolve the types in my jar file.
When I then run mvn install again (the .jar file now already exists in the lib directory before the build), it works fine.
Any ideas how to resolve this?
Short answer
Currently it does not work as expected, because of bugs in Tycho
#353889: Defer target&dependency resolution to the normal build
#393978 maven-dependency-plugin:copy-dependencies goal does not work reliably with Tycho projects - "error copying ....jar.jar"
Long answer
Here is what I did to make it work (for now) in the xxx.dsl project:
pom.xml file
I use the maven-dependency-plugin to download the jar file in the maven validate phase (as early in the build as possible) to the lib directory.
Note, that I use stripVersion=true so that the file in the lib dir is called value-annotations.jar (and not value-2.5.6-annotations.jar). If I ever want to update the version in the future, I only need to update it in one place in the pom.xml file.
The jar file must also be specified as a dependency, because otherwise the users of the dsl plugin cannot build the project: i.e. the generateXtext task of the xtext-gradle-plugin will fail because it cannot find the classes in the jar file.
Relevant pom.xml code:
<project ...>
<properties>
<xtextVersion>2.13.0</xtextVersion>
<immutablesVersion>2.5.6</immutablesVersion>
...
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-libraries</id>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>${immutablesVersion}</version>
<classifier>annotations</classifier>
<outputDirectory>lib</outputDirectory>
</artifactItem>
</artifactItems>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
...
</build>
<dependencies>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>${immutablesVersion}</version>
<classifier>annotations</classifier>
</dependency>
</dependencies>
</project>
META-INF/MANIFEST.MF file
Add the jar file to the Bundle-ClassPath, so that we can use it: e.g. in the DslJvmModelInferrer.xtend
Add the package of the jar file to Export-Package, so that these files can be accessed by the xxx.dsl.tests project
Relevant parts of MANIFEST.MF:
Bundle-ClassPath: ., lib/value-annotations.jar
Export-Package: xxx.xtext,
...
xxx.xtext.validation,
org.immutables.value
build.properties file
Add the jar file to the bin.includes so that it will be copied to the generated jar file (in the target directory):
bin.includes=model/generated/,\
.,\
META-INF/,\
lib/value-annotations.jar,\
plugin.xml
Build
Now the build works in Eclipse.
On the command line (and in my continuous integration server script), I must execute maven twice (because of the mentioned bugs):
mvn verify (to download the jars)
mvn install

Maven dependencies inside tar.gz file

So I have the following tar.gz file in my repo with structure as:
> A.tar.gz
> |
> |____ a.tar.gz
> |
> |____ b.tar.gz
> |
> |_____ folderA
> |
> |_____ folderB
> |
> |______ jar1.jar
> |
> |______ jar2.jar
Now in my POM file for another project I would like to add the jar1 and jar2 as dependencies. So far I have the following:
<dependency>
<groupId>com.groupid</groupId>
<artifactId>master</artifactId>
<version>18.1</version>
<type>tar.gz</type>
<classifier>bin</classifier>
</dependency>
This made the tar file available. I then tried to unpack as:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeTypes>tar.gz</includeTypes>
<includeArtifactIds>master</includeArtifactIds>
<outputDirectory>target/somefolder</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
However, on running the build, I still don't get the jars as dependencies. I am sure I am missing something , so any help appreciated!
So you have JAR dependencies located inside another tar.gz dependency that you want in your project. So far so good, the problem is you're trying to:
Make the the first .tar.gz dependency available (OK)
Launch the build to unpack the jars (OK)
Add unpacked jar as dependencies during the same build (Not possible)
All in one run. That is not possible in Maven by design (to the best of my knowledge). Even if you did find a way to do it this way, that would over-complexify your build and break Maven design, probably leading to other issues.
You mentioned that you don't control the packaging of the other team and it seems you can't upload said dependencies on a Nexus repo either. What you can do is make the JAR dependencies available in your local repo prior to running your build by doing something like:
Download your tar.gz file and unpack it
Install the jar dependencies in your local Maven repo using commands like mvn install:install-file -Dfile=path/to/jar1.jar -DgroupId=com.mycompany -DartifactId=jar1 -Dversion=1.0.0 -Dpackaging=jar with proper version, groupdId and artifactId for each jars (see the Guide to installing 3rd party JARs for more details)
Now you can run your your original project only by mentioning your jar dependencies as <dependency>
With this you will manually install your jar dependencies in your Maven local repository, rendering them available to your project without needing a Nexus repository or further unpackaging. You can perform step 1 and 2 manually or by creating another Maven project that you will have to run once before running your main project. To do so you can create a new project and use the maven-dependency-plugin as you already did coupled with the maven-exec-plugin to run the mvn install:install-file command.
Note this process must be done for every individual machine on which you will run your project. As #khmarbaise mentioned, it's best to have your dependencies available directly through a repository manager such as Nexus without having to perform additional steps, but this temporary workaround should work just fine.

maven release:perform with Perforce does not create target/checkout directory

I can not make the maven-release plugin work with Perforce. The release:prepare seems to work correctly: a Perforce label is created and it contains the correct files. Yet when I run release:perform it fails because the target/checkout directory is empty.
I've done some experiments. If I only sync to my pom.xml and then run
mvn scm:checkout
then my other files are checked out into the P4 root directory just as if I had done a
p4 sync ...
Yet Maven outputs
[INFO] Checkout working directory: /home/chris/perforce/pips/target/checkout
[INFO] sync -f //depot/pips/...
Maven thinks it's checking out the target/checkout but it is not. I dug into the P4Maven code a little bit and noticed this in P4Executor.java
public String getRepoPath(P4ScmProviderRepository repo, File basedir) {
// Handles a case where executing release:prepare on a module with an
// invalid SCM location. The release.properties contains the invalid URL
// for checkout during release:perform and the basedir is not the module
// root. So, the trailing target/checkout directory must be removed.
if (basedir.toString().replace('\\', '/').endsWith("/target/checkout")) {
String dir = basedir.toString();
basedir = new File(dir.substring(0, dir.length()
- "/target/checkout".length()));
if (getLogger().isDebugEnabled()) {
logger.debug("Fixing checkout URL: " + basedir);
}
}
This code is pretty clear. P4Maven won't check things out to target/checkout. If you tell it to checkout to target/checkout it will simply remove the "target/checkout" and checkout to the root directory. This is consistent with what I see being done. I also see the "Fixing checkout URL" message when I run release:perform
How then do people work around this?
Below is my pom.xml. I am using
Perforce Server version: P4D/LINUX26X86_64/2014.2/935585 (2014/09/16). p4d is running locally. Authentication is disabled and I don't need a password.
Nexus 2.10.0-02 running locally.
Ubuntu Linux.
Maven 3.2.3
java 1.7.0_55
Thanks.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.ap
ache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.icap</groupId>
<artifactId>pips</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>pips</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<scm>
<connection>scm:p4:192.168.1.8:1666://depot/pips</connection>
<developerConnection>scm:p4:192.168.1.8:1666://depot/pips</developerConnection>
</scm>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.4</version>
<dependencies>
<dependency>
<groupId>com.perforce</groupId>
<artifactId>p4maven</artifactId>
<version>[2011,2012)</version>
</dependency>
</dependencies>
<configuration>
<connectionType>connection</connectionType>
<username>chelck</username>
<includes>**</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.1</version>
<dependencies>
<!-- P4Maven -->
<dependency>
<groupId>com.perforce</groupId>
<artifactId>p4maven</artifactId>
<version>[2011,2012)</version>
</dependency>
</dependencies>
<configuration>
<connectionType>connection</connectionType>
<username>chelck</username>
<includes>**</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<fork>true</fork>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<repository>
<id>deployment</id>
<name>Internal Releases</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>deployment</id>
<name>Internal Releases</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
I have struggled for several days on perforce / maven / release integration and finally got it working. Here are my findings, they might help you and others with similar issues.
Do not use the perforce plugin as described on perforce.com (http://www.perforce.com/perforce/r11.1/manuals/p4maven/index.html). Although it is the first link to come out of a search on google, my understanding is that this plugin is outdated.
In your configuration, if you refer to scm:p4:... you are using the com.perforce plugin. Instead, use org.apache.maven.scm/maven-scm-provider-perforce which uses scm:perforce:... syntax. Your <scm> should look like this:
<properties>
<p4path>//perforce-path/toyour/project</p4path>
</properties>
<scm>
<connection>scm:perforce:yourp4server:1666:${p4path}</connection>
<developerConnection>scm:perforce:yourp4server:1666:${p4path}</developerConnection>
<url>perforce:yourp4server:1666:${p4path}</url>
</scm>
The newer plugin does not require username/password, it will use your active P4 credentials.
I am using maven 3.2.5 and did not have to specify any plugin dependency. The built-in scm recognized the :perforce: scm and downloaded maven-scm-provider-perforce 1.9.2 which is the latest as of this writing. You can make sure you are using an up to date plugin by deleting the maven-scm-provider-perforce folder in .m2\repository\org\apache\maven\scm and then doing a mvn scm:status - it will download the missing plugin and you can see which one it picked.
Here is my maven-release-plugin definition:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.1</version>
</plugin>
I do not have a maven-scm-plugin definition in my pom (I used the default settiings from the superpom).
Test on a non-module project.
You should be able to do the following without error:
- mvn scm:status to check that your scm setup is working correctly
- mvn release:prepare
- mvn release:perform
If you get errors, rollback (mvn release:rollback), make sure everything is back to normal, and rerun with mvn -X to get more details.
In my case, a non-modular project worked without much trouble.
Test on the parent of a reactor project.
There are a few things that were not obvious to me at first and that I found out:
When the is defined in the parent pom, the child module name gets appended to the path for the child module. If your child is not physically under the parent (in my case, it was beside), you need to redefine a configuration in the child pom. In my case, I was using a property (p4path above), simply changing its value was not enough. This was counter-intuitive since the parent pom is normally just a "include", and I expected the parent definitions to be "copy-pasted" in the child module, but in this particular case, it is modified to include the module name at the end of the scm path when it is copy-pasted. [More on modules that do not sit under the parent directory below]
A temporary client spec is created by the perforce scm plugin. This was also counter intuitive, I expected the scm plugin to use my current client-spec. Instead, it will create a new one on the fly, and populate it with the path you provided only and name it username-hostname-MavenSCM-localpath. I was initially surprised by that and tried to change it so it would use by client-spec by customizing the maven.scm.perforce.clientspec.name property, this behaviour was actually correct. With this temporary client-spec, the plugin can limit the lookup for changes, the submit and tagging to this directory. It also makes it easy to track what was done by this plugin in the perforce history.
Creation of the temporary client spec will fail if the modules are not under the parent directory. As I described above, my modules were sitting beside the parent. My <module> tag in the parent pom referred to them using <module>../mymodule</module> which worked fine in eclipse for everything else. The scm perforce plugin includes modules in the client spec by appending the path to the module to the path of the parent. Since Maven resolves the ../ to an absolute path, the client spec ends up containing something like this: //perforce-path/toyour/project/C:\Path\To\Your\Project/mymodule/ and the client spec fails. I could not find a work around for this issue other than moving my modules under their parent. Since the client spec was invalid, I had no file being checked-out under target/checkout. You will get the content of your generated client spec on the console by using mvn -X when invoking release:perform.
Moral of the story: You want to make sure that all your modules sit under their parent directory. When you do this, everything becomes simple and magical: you can leave the declaration in your parent pom and the module path gets magically appended for the module scm paths. You can refer to your modules using <module>mymodule</module> which gets correctly appended in the client-spec. Tagging of the parent in perforce ends up also tagging the modules since they are contained within.

Adding db2 jars to java webapp using maven

I'm trying to add the following db2 jars to my Java web application using Maven...
db2jcc_license_cu.jar
db2jcc_javax.jar
db2jcc.jar
I'm following the instructions posted in this post...
Can I add jars to maven 2 build classpath without installing them?
I want to use the static in-project repository solution. So far I have...
Created a folder in my root directory named lib. Inside this
directory lives the three db2 jars.
Added the following to my pom file...
<repository>
<id>lib</id>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc</artifactId>
<version>3.8.47</version>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc_license_cu</artifactId>
<version>3.8.47</version>
</dependency>
But when I run a maven install I get ...
[WARNING] The POM for com.ibm.db2.jcc:db2jcc:jar:3.8.47 is missing, no dependency information available
[WARNING] The POM for com.ibm.db2.jcc:db2jcc_license_cu:jar:3.8.47 is missing, no dependency information available
I got the version of the Jars by running a...
java com.ibm.db2.jcc.DB2Jcc -version
Have I specified this version info corretly? Can anyone suggest what I am doing wrong?
The problem is that you didn't install the jars properly in your "project-maven-repository" (i.e. in the folder ${project.basedir}/lib)
Maven stores (when you do mvn install) the jar files in a maven repository. A maven repository have precise hierarchical structure. Here is a simplified vision of this structure:
the artifact groupId+artifactId define the first part of folder path (in the repository) where the artifact is stored.
the artifact version is the second part of the folder path
the artifact version is also a suffix to the artifact name
the artifactId is the artifact name
the packaging is the artifact extension (default is jar)
By default maven use a repository located under <USER_HOME>/.m2/repository
The solution you are trying to setup use another location for the repository : ${project.basedir}/lib and even if it is not the default repository location it is still a maven-repository and so maven is expecting to find the usual maven repository hierarchy under this location.
That's why you need to organize your ${project.basedir}/lib folder just like a maven repository. That's explained in this part of the referenced post:
Use Maven to install to project repo
Instead of creating this structure by hand I recommend to use a Maven plugin to install your jars as artifacts. So, to install an artifact to an in-project repository under repo folder execute:
mvn install:install-file -DlocalRepositoryPath=lib -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]
If you'll choose this approach you'll be able to simplify the repository declaration in pom to:
<repository>
<id>repo</id>
<url>file://${project.basedir}/lib</url>
</repository>
So you need to do an mvn install to create the ${project.basedir}/lib hierarchy (you can do it by hand, but it's not recommended and error prone).
I your case, the commands to run will be like this: (assuming you put the jar in your HOME_DIR and run this command in your ${project.basedir})
mvn install:install-file -DlocalRepositoryPath=lib -DcreateChecksum=true -Dpackaging=jar -Dfile=<USER_HOME>/db2jcc_license_cu.jar -DgroupId=com.ibm.db2.jcc -DartifactId=db2jcc_license_cu -Dversion=3.8.47
What are the advantages of the approch you choose :
a developer with no maven setup will have the libraries available inside the project sources, under SCM system.
you can easily reference jars that aren't in a public maven repository without the need of something like artifactory or nexus
The drawbacks :
a quite complex folder structure under ${project.basedir}/lib looking very strange for someone not used to work with maven.
you will store the libraries under SCM (lot's of huge binary files)
Another solution would be to download those jars before hand and put them somewhere relatively to your project (like lib directory). Now just tell maven to use those jars. Here the groupId, artifactdId and version are JFYI since they won't be used to download anything.
The merit of this solution is that you won't have to build a maven repository.
<dependencies>
...
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>licences</artifactId>
<version>1.0</version> <!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc_license_cu.jar</systemPath>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>1.0</version> <!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc4.jar</systemPath>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc_javax</artifactId>
<version>1.0</version> <!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc_javax.jar</systemPath>
</dependency>
</dependencies>
Refer Link (Japanese): Mavenリポジトリで提供されていないサードパーティJarをどうするか
I guess these jars do not have a pom.xml. Hence the warning. If the jars get packaged and the application works, then I guess you do not have a problem.

Resources