I am having a group of external jars(in hundreds) which I have added in the build path of my project to make it work.
But while packaging it is failing as these jar's are not available to maven.
I have gone though many articles and all the solutions(like adding the jar at system path) are for a single jar only.
<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>
Is there any way we can add the group of jars(folder) to the packaging on the project? or any other solution by which my project can build successfully?
can we create a single jar containing all my jars inside and then use the system scope of maven
I have tried creating jar by jar -cvf my_jar.jar * and placed this jar in the system scope. But it does not worked for me.
My solution : Maven pluggin addjar let us add all jar at a place(projectdirectory/lib in this case).
this enables you to add these jar's in the final package(jar in my case) when you maven build, but to run locally you have to add those jar files directly in the classpath.
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.5</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${basedir}/lib</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Now create a shade jar using mvn clean install shade:shade
The bad news: For a proper Maven project, you need to add each and every artifact you use as <dependency> in your POM.
The good news: I very much doubt that these 100 jars are all
- directly used in your source code
- not available in a public Maven repository like MavenCentral
So the better strategy would be to figure out what you really need and find that in MavenCentral. Then Maven finds all the transitive dependencies for you. So if you really need 10 of the jars and all other jars are just dependencies of your dependencies, just add these 10 ones (from MavenCentral) and you are done.
Related
After creating my war file with mvn package, war file include web/INF lib folder. In that folder, there are some jars which I did not prefer because of the versions of jar.
I cannot manage the version of these jars directly. Some spring plugins/packages manages this.
I tried to increase the versions of plugins which in pom.xml but it did not bring exact version which I need.
How can I change it with mvn commands or pom.xml?
You can get a clear view of the hierarchy of dependencies with this command:
mvn dependency:tree
To exclude a jar from the war, you need to specify the scope with "provided" value. So, you need to add (for a dependency of a dependency) or change the corresponding dependency block:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
Another way is to configure the plugin that does the magic:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<packagingExcludes>WEB-INF/lib/spring-aop-5.3.19.jar</packagingExcludes>
</configuration>
</plugin>
Your project have some jars in web/INF lib,there can not change with mvn.
You should find this jars what you want version in internet,download and replace.
But I suggest you use mvn manage this jars.
I am writing a project for acceptance testing and for various reasons this is dependent on another project which is packaged as a WAR. I have managed to unpack the WAR using the maven-dependency-plugin, but I cannot get my project to include the unpacked WEB-INF/lib/*.jar and WEB-INF/classes/* to be included on the classpath so the build fails. Is there a way to include these files into the classpath, or is there a better way of depending on a WAR?
Many thanks.
There's another option since maven-war-plugin 2.1-alpha-2. In your WAR project:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
This creates a classes artifact which you can use in the acceptance tests project with:
<dependency>
<groupId>your-group-id</groupId>
<artifactId>your-artifact-id</artifactId>
<version>your-version</version>
<classifier>classes</classifier>
</dependency>
Indeed, by design, Maven doesn't resolve transitive dependencies of a war declared as dependency of a project. There is actually an issue about that, MNG-1991, but it won't be solved in Maven 2.x and I'm not sure that I don't know if overlays allow to workaround this issue. My understanding of the suggested solution is to duplicate the dependencies, for example in a project of type pom.
(EDIT: After some more digging, I found something interesting in this thread that I'm quoting below:
I have been helping out with the development of the AppFuse project over
the last month where we make heavy use of the war overlay feature in the
Maven war plugin. It is a really nifty feature!
To get max power with war overlays I have developed the Warpath plugin
that allows projects to use war artifacts as fully fledged dependencies.
In brief:
1) The contents of the /WEB-INF/classes directory in the war dependency
artifacts can be included in the project's classpath for normal compile,
etc tasks.
2) Transitive dependencies from the war dependency artifacts become
available for use by other plugins, e.g. compile and ear - so no more
having to include all the dependencies when creating skinny wars!
The plugin has now been actively used in the AppFuse project for the
last few months, and I feel it is at a point where it is both usable and
stable.
Would the war plugin team be interested in including the warpath
functionality inside the war plugin? It would seem to be the most
natural place to host it.
So, I don't have any experience with it, but the maven warpath plugin actually looks nice and simple and is available in the central repo. To use it,include the following plugin configuration element in your pom.xml file:
[...]
<build>
<plugins>
<plugin>
<groupId>org.appfuse</groupId>
<artifactId>maven-warpath-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>add-classes</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
And add the war dependencies you want included in the classpath as warpath type dependencies:
[...]
<dependencies>
<dependency>
<groupId>org.appfuse</groupId>
<artifactId>appfuse-web</artifactId>
<version>2.0</version>
<type>war</type>
</dependency>
<dependency>
<groupId>org.appfuse</groupId>
<artifactId>appfuse-web</artifactId>
<version>2.0</version>
<type>warpath</type>
</dependency>
</dependencies>
[...]
Both the war and warpath dependency types are needed: the war type is used by the Maven war plugin to do the war overlay, the warpath type is used by the Warpath plugin to determine the correct list of artifacts for inclusion in the project classpath.
I'd give it a try.)
Use overlays. First, your test project need to have also packaging war.
Declare dependency of war project you want to test:
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>your-project-arftifactId</artifactId>
<version>${project.version}</version>
<type>war</type>
<scope>test</scope>
</dependency>
then configure maven-war-plugin overlay:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${basedir}/src/main/webresources</directory>
<filtering>true</filtering>
</resource>
</webResources>
<overlays>
<overlay/>
<overlay>
<groupId>your.group</groupId>
<artifactId>your-project-artifactId</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
In the above example in test project I overwrite webresources configuration files (like conxtext etc.).
EDIT: This solution wasn't tested with Maven 3.
Good point, Justin. That got me actually solving my problem, namely: including a war into an assembly AND including all its transitive dependencies.
I could not duplicate the war-dependency as 'jar' as you suggested since the assembly plugin would not find a jar referenced by that groupId/artefactId, but
duplicating the war-dependency as type pom
works!
The war and its transitive dependencies are not included in the assembly.
To exclude the (now also appearing) pom file I had to add an exclude element like this:
<excludes>
<exclude>*:pom</exclude>
</excludes>
into my assembly.xml file.
I think this could also be a workaround for the original question of this thread.
If you list the dependency on the war project as a jar dependency it seems to pickup the required jars/resources. I'm using Maven 2.2 + m2eclipse.
How can I add an external jar file as a dependency for a Maven project in IntelliJ IDEA? Because when I add it in the dependency list and try to compile with Maven, I got an error that that dependency couldn't be found.
You can either
define a system/local dependency like this:
<dependency>
<groupId>example</groupId>
<artifactId>example</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>lib/example-1.0.0.jar</systemPath>
</dependency>
As Gimby pointed out, be aware that system dependencies are expected to 'just be there', so they will not be packaged and deployed with your artifact. See this question for reference.
install the artifact into your local repo:
mvn install:install-file -Dfile=<path-to-file> \
-DgroupId=<myGroup> \
-DartifactId=<myArtifactId> \
-Dversion=<myVersion> \
-Dpackaging=<myPackaging> \
-DlocalRepositoryPath=<path-to-my-repo>
Ideally you should deploy the JAR to your repository using mvn deploy:deploy-file.
If that's not possible, you can set the dependencies scope to system and then include a systemPath in the dependency which gives that path to the jar. This is explained in POM Reference - dependencies and comes with a warning that any artifact that depends on the artifact with the system scope dependency will also expect to find the jar via the systemPath.
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.
So my maven project depends on a jar that is NOT in any maven repository. Therefore I need to use the system scope to include this local file in my maven classpath. Question is: When I build my final jar to distribute my library, I do need to somehow include that dependency with it. The classes can be extracted and then bundled with my library classes OR the jar can be included somehow inside my library jar. I am not even sure the latter (jar inside jar) is possible in Java.
So how should I approach this problem? Will Maven take my system scope dependency and take care of that for me? What should I do?
Thanks!
No, it will not. These dependencies are only for compilation and transitive dependency resolution. Your library consumers should have the jar too.
However, you could use the Assembly Plugin to repackage the jar's classes into your artifact.
Or, the standard approach: then you will publish your artifact, you will create a public repository for deployment. You also could deploy the jar in it.
UDPATE: adding example for the shade plugin (instead of the Assembly)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>legacy-jar.groupId:legacyJar</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
There is a second option. Put the jar into your repository as described here:
http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
and to your online repository
http://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
give your client access to your repository and use your legacy.jar as normal dependency.
It depends a little bit what kind of library do you have. With this way you don't have problems with version conflicts of your legacy.jar in the environment of your customer.
Could this be what you are looking for, http://maven.40175.n5.nabble.com/How-to-specify-local-dependency-in-maven2-td103415.html.
We have a number of third party dependencies that aren't hosted anywhere. For each of these we have a jar file that we'd like to be able to install and/or deploy to our repository. Some of the jar files have their own dependencies and we also need to declare these.
We've made pom.xml files for each jar file that declare the groupId, artifactId, dependencies, etc. These pom.xml files all have a common parent pom that declares some of the common info (e.g. <repositories> and <distributionManagement>).
I'd like to be able to install or deploy these dependencies with something as simple as mvn install and mvn deploy (or maybe mvn install:install-file and mvn deploy:deploy-file) and have all the necessary properties for these commands (artifactId, repositoryId, etc.) be read from the pom.xml files.
To get this to work, at least for deploying, I tried putting the following in my parent pom:
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.4</version>
<configuration>
<file>${jarfile}</file>
<pomFile>pom.xml</pomFile>
<repositoryId>our-id</repositoryId>
<url>our-url</url>
</configuration>
</plugin>
</plugins>
</build>
and then having each of the child poms define the jarfile property. That allows me to run mvn deploy:deploy-file to deploy all the child pom artifacts. Presumably I could do something similar to get mvn install:install-file to work.
But with this approach, I'm unable to release the parent pom (which I must do since the child poms depend on it), and if I try to mvn release:perform on the parent pom, I get errors like:
Cannot override read-only parameter: pomFile
I feel like I'm probably going about this the wrong way. All I really want to do is:
Put the common code for all the third party jars in one shared parent pom
Write an additional minimal pom for each third party jar
Be able to run something like mvn install or mvn deploy without having to specify all those complicated command line properties
How can I best accomplish that?
Edit: Made it clearer above that ideally I'd like to be able to run something as simple as mvn install or mvn deploy and not have to specify properties on the command line.
When Maven is missing a dependency, it will give you an error in it's output that contains the command line to issue to add the jar to a remote repository. That command line will automatically create a POM for you and upload the two together. Is that not sufficient for your needs?
Using this facility, if I know that I have a dependency with no remote repository representation, I usually just go ahead and define it in my build with the GAV that I want, run the build once, then look at the build errors. Copy out the command for the deployment, then run that command. You'll want to make sure that you have the <repository> elements set up properly in the parent POM plus also set up corresponding <server> elements in your settings.xml with the repository upload password. Other than that, you should be good to go.
I would add that you should check out Nexus before getting to far. It's worth the hassle to set up now! :-)
Ok, I found a solution that allows me to run just mvn install or mvn deploy and have the jar file installed to the local or remote repository. Inspired by a post to the maven-users list and using the build-helper plugin, in the parent pom, I have:
<pluginManagement>
<plugins>
<!-- Attach the jar file to the artifact -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${artifactId}-${version}.jar</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
And then in the child poms, I have:
<packaging>pom</packaging>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Some of the pieces of this that initially tripped me up:
The attach-artifact execution should be under <pluginManagement> so it doesn't get executed if you mvn install or mvn deploy the parent pom.
The children need to specify the build-helper-maven-plugin under the build plugins so that code from the parent <pluginManagement> gets run.
The children have to be declared as having <packaging>pom</packaging> because you can't add a jar to an artifact if it has the same name as the artifact.
The only downside I see to this approach is that the artifact gets deployed as type pom instead of type jar. But I haven't seen any real consequences of that.
I meet this problem in my work:
Now I have a target.jar (it has a dependencies list : a.jar, b.jar, c.jar...), I want to use mvn install:install-file to put it into my local repo, but when I run the command blow
mvn install:install-file -Dfile=/Users/username/.../target.jar -DgroupId=com.cnetwork.relevance -DartifactId=target -Dversion=1.0.0
but when I use it I found there are many error, the jar which use target.jar cannot find a.jar, b.jar, c.jar, such as:
com.cnetwork.a does not exist
com.cnetwork.b does not exist
com.cnetwork.c does not exist
Then I went to ~/.m2/repository/.../target/1.0.0/target.pom to find the pom file of the target, but nothing in it!
...
<groupId>com.cnetwork.relevance</groupId>
<artifactId>target</artifactId>
<version>1.0.0</version>
....
# no dependencies about a/b/c.jar !!!
This is what going wrong, the install:file -Dfile -DgroupId -D.. does not add dependencies into pom, I correct the answer by this method
if you already have this maven project source, just install it into local repo
mvn clean install
if you have the jar and the pom of it, install jar with pom
mvn install:install-file -Dfile=/.../.../target.jar -DpomFile=/.../../target.pom
if you don't have a pom with target jar, write one and use upper command.
if you cannot recover the pom file of it, may be you should give up.