I have a Maven 3.3 project, and the main output is a war file:
<artifactId>pro</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
I am using the maven plugin to also build a jar file, which goes into target/pro-1.0-SNAPSHOT.jar and this works.
I would like to install this jar to the local maven repo, so I'm using the maven install plugin to do this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<classifier>jar</classifier>
<packaging>jar</packaging>
<file>target/pro-1.0-SNAPSHOT.jar</file>
</configuration>
<executions>
<execution>
<id>do-jar-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
The whole build works fine except the jar install step is installing the war, and not the jar. How do I override this?
I'm looking here to see what I can use: http://maven.apache.org/plugins/maven-install-plugin/examples/installing-secondary-artifacts.html
Here is the log from my build:
**
Building jar: /Users/mike/code/workspace/pro/target/pro-1.0-SNAPSHOT.jar
[INFO] --- maven-install-plugin:2.5.2:install (do-jar-install) # pro
[INFO] Installing /Users/mike/code/workspace/pro/target/pro-1.0-SNAPSHOT.jar to /Users/mike/.m2/repository/net/mikeski/pro/1.0-SNAPSHOT/pro-1.0-SNAPSHOT.war
Note the last line - it's picking up the jar but installing a war
How can I fix that?
As requested, here is the jar plugin config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>create-jar</id>
<phase>install</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
It creates a jar of the files in the project, and it's the correct jar. If I copy the war that is installed in my repo to a jar (so I just change the extension) my other project can pick it up just fine.
Your current output is the result of multiple considerations, one of them being maven-jar-plugin too permissive in version 2.4.
First of all, you need to remember that inside a Maven repository, all the artifacts share a single naming convention, which is artifactId-version(-classifier).packaging. This means that whatever the local name of the file your build is producing (let it be foo.jar), it will be installed and it will be deployed with this conventional name. All that matters when artifacts are installed are the Maven coordinates, i.e. the groupId, the artifactId, the version, the classifier and the packaging.
What is happening here is:
Your project has a packaging war. Running Maven with mvn install, the default-install phase will be invoked first and the maven-install-plugin:install goal will be run a first time, installing your WAR project. On your logs, you will find:
[INFO] --- maven-install-plugin:2.5.2:install (default-install) # test-war ---
[INFO] Installing ...\test-war\target\test-war-0.0.1-SNAPSHOT.war to ...\test-war\0.0.1-SNAPSHOT\test-war-0.0.1-SNAPSHOT.war
[INFO] Installing ...\test-war\pom.xml to ...\test-war\0.0.1-SNAPSHOT\test-war-0.0.1-SNAPSHOT.pom
Then, you are using the maven-jar-plugin:jar goal to create a JAR. This plugin creates a JAR for the current Maven project - so it will create it alright, but the Maven coordinates of this new artifact will be exactly the same as those of your WAR project (you didn't specify a classifier). Therefore, you effectively replace the file of the main artifact (which is a WAR), by a JAR file: you end up with a local file having an extension of jar (because the maven-jar-plugin created it this way) that is the file of the main artifact of a Maven project of packaging war. Quite confusing.
Remember that I said that the maven-jar-plugin was too permissive? If you update to version 3.0.2 of the plugin, you will get an error right here (MJAR-198):
You have to use a classifier to attach supplemental artifacts to the project instead of replacing them. -> [Help 1]
which summarizes what is said above.
Finally, you declared another execution of the maven-install-plugin called do-jar-install, that is supposed to install this local JAR file. And this is what it does: it installs the local JAR file inside your target folder to your local Maven repository using the coordinates of the artifact. The confusion comes from the fact that the type (packaging) of the artifact is in fact WAR, so what gets installed is a WAR file (being effectively a JAR)...
Now that we've explained the issue, the question is: what do you want to do? It looks like you want to attach to your WAR project an additional artifact composed of the classes of it. There is no need for all this configuration, you can just use the attachClasses parameter of the maven-war-plugin.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
You received this because double invocation of plugin :
execution phase
package - default phase
If there is no strong reason and all you care is to have just one JAR as artifact from the project, use one among them.
something like :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${outputDirectory}</outputDirectory>
</configuration>
</plugin>
Related
I am having a problem with maven. I have included a dependency as such in my pom.xml:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.6</version>
</dependency>
I am using Intellij as an IDE, and I get no compile warnings there or anything. I am using the command line to run the maven commands, and I can run mvn install compile package all without trouble.
However, when I try running the jar as such:
java -cp target/stride-1.0-SNAPSHOT.jar com.myapp.maven.App
I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVFormat
at com.stride.maven.App.parseCsv(App.java:43)
at com.stride.maven.App.main(App.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.csv.CSVFormat
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Clearly, maven cannot find that path. I tried deleting the .m2, rebuilding, and mostly everything I have found on stackoverflow, but I cannot find the issue, or get visibility into the issue. Note, in my Intellij I can see the dependency in the external libraries.
I have also tried using shade to copy the dependancies to the jar:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation=
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.myapp.maven.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Following which I reran mvn clean install package and then my build command. No luck.
I have also tried Maven Assembly plugin. Guess what, no luck!
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.myapp.maven.App
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
I confirmed the dependencies are not even getting built in.
Thanks!
Maven works great in this case.
However, you seem to miss the point of dependency.
When you define a dependency maven uses it during the compilation (hence no errors in both intelliJ and when you execute mvn install)
But this doesn't mean that dependency is placed right into the jar.
There are more complicated packaging types of application where it indeed works like this (dependant jars are included into the artifact) for example Spring boot application, Web Archives (WAR) and so forth. But since you compile a regular JAR it will include only the classes of your module and won't include classes of commons-csv in this case.
So in order to be able to run this application you should chose one of:
Put Jar of commons-csv (as well as other dependencies that you might have) into class path: java -classpath <path-to-commons-csv> -jar YourApp
Use Maven Shade Plugin to create a jar that will indeed include all the dependencies as classes
As mentioned in the other answers, you need to package up your dependencies in your executable jar (a.k.a. Uber-JAR). You mention you have tried two ways: using the Maven assembly with a descriptorRef jar-with-dependencies and the Maven shade plugin.
First global remark: Maven plugin configuration can be defined in a <pluginManagement> block or directly in the <build> part of a POM. I would suggest to put them in the <build>, and I assume you already put them there, but I can't verify that since you only pasted a part of it in your question.
When using the jar-with-dependencies descriptorRef in the Maven assembly plugin, two jar files will be created in your target/ folder: stride-1.0-SNAPSHOT.jar and stride-1.0-SNAPSHOT-with-dependencies.jar. You should use the -with-dependencies.jar, so run java -cp target/stride-1.0-SNAPSHOT-with-dependencies.jar com.myapp.maven.App
The Shade plugin offers more options than the Maven assembly plugin. However in the code you pasted you have not bound the execution of the Maven Shade plugin to the package phase of Maven's lifecycle. If you run a mvn package you will NOT see the Maven shade plugin as part of the build steps that Maven did. If you look more closely to mkyongs guide, you'll see that you need to include a <phase>package</phase> in your <execution> block.
Hi the problem is that when the jar is getting created the dependencies being downloaded is not being attached to the executable jar that is why there error is showing up we can add the below section in the build tag to get the dependencies attached to the executable jar and then you can execute the jar with java -jar command
maven-assembly-plugin
package single
... wow.dxdatagenerator.App
jar-with-dependencies
-->
We have an Eclipse Plugin which we build using Maven and Tycho. Currently
however, we still provide all project dependencies through a bunch of manually
added JAR files and not by Maven. This is due to the following reasons: (1) The
dependencies are not available through a standard Eclipse update site (at least
not in a current version), (2) the dependencies are not available as bundles.
The biggest part of these dependencies are the Selenium libraries (API, Remote,
browser-specific libs and their transitive dependencies, such as Guava, etc.)
I've wasted hours, trying to pull those dependencies during our Maven build.
Following this SO question, I tried the p2-maven-plugin, created an update
site with our dependencies which I added to my Eclipse target platform. However,
during runtime, classes, which are referenced across different JARs could not be
loaded (I assume, from my very limited OSGi knowledge, because some
necessary information was missing in the MANIFEST.MF files). Here's an example
of the issue, when trying to create a RemoteWebDriver, which uses the
DesiredCapabilities class (both classes in different bundles):
Exception in thread "Thread-8" java.lang.NoClassDefFoundError: org/openqa/selenium/remote/DesiredCapabilities
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:243)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:126)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:153)
…
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.DesiredCapabilities cannot be found by org.seleniumhq.selenium.remote-driver_2.45.0
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:439)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:352)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:344)
at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Is there anything I still need to take care of, when using the p2-maven-plugin? The relevant parts of the pom.xml looked like this:
<plugin>
<groupId>org.reficio</groupId>
<artifactId>p2-maven-plugin</artifactId>
<version>1.1.1-SNAPSHOT</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<artifacts>
<artifact>
<id>org.seleniumhq.selenium:selenium-remote-driver:2.45.0</id>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
Couldn't get it to work, so we're now using the maven-dependency-plugin with the copy-dependencies, which we execute during the Maven initialize phase to pull all necessary dependencies (contrary to my initial feeling, this can be combined with the pom.xml using the eclipse-plugin packaging and the "manifest first" approach). The relevant part looks like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>initialize</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The Maven dependencies are then copied to target/dependency.
Only little issue: The Bundle-ClassPath in the MANIFEST.MF needs to be manually updated in case the name of a JAR file changes when updating Maven dependencies (e.g. commons-io-2.4.jar becomes commons-io-2.5.jar).
[edit] Revisiting this answer in regards to the last sentence above: The version numbers can be conveniently stripped through the following option: <stripVersion>true</stripVersion>. This means, the above library will be renamed to commons-io.jar and thus no paths need to be updated when a version number changes.
Another possibility:
Some jar files may be broken (if you're using Eclipse, it's commonplace hibernate-commons-annotations-4.0.1.Final.jar; invalid LOC header (bad signature)? ). To check this possibility, try manually opening the jar to see if it's okay.
I also build an Eclipse plugin with Maven and Tycho. I have the same problem: the bundle org.eclipse.team.svn.core and org.eclipse.team.svn.ui are not available through a standard Eclipse update site.
Maybe you can try this to solve this kind of problem:
In Dependencies, find the box Automated Management of
Dependencies.
Add the wanted plugin using Add...
Choose Analyze code and add dependencies to the MANIFEST.MF via: Import-Package
Click on Add Dependencies so that you find required packages in the box Imported Packages nearby.
Then you can run the Maven build.
We have a maven project in which we use some artifacts that are not present in any remote repositories. They are included in our project in some directory, say /lib, as compiled .jar files. Some of these are "plain" dependencies which we can utilize from /lib using scope system + systemPath, however there is one artifact that should be used with the maven-dependency-plugin unpack goal. The relevant part of the pom.xml looks like this
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>xxx.yyy.zzz</groupId>
<artifactId>ourartifact</artifactId>
<outputDirectory>${target.directory}/somedir</outputDirectory>
<includes>
files1/**,files2/**,files3/**
</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
However this still tries to reach out to the remote repository and fetch the artifact from there, which of course does not succeed. Can we somehow achieve that this artifact is also attempted to be fetched from /lib?
Instead of fetching from /lib, and if setting up your own Nexus/Artifactory/etc... is a bit much right now, you could add the jar to your local repository. Each individual would have to do this on their own computer, but once there, it would be available to all the maven built projects on that machine, so you wouldn't have to have a /lib copy for each project. If you are compiling that jar yourself and it is a maven project, you can do "mvn install" and that will install to your local repo. Note that mvn package won't install to your local repo.
If this is a 3rd party jar, you can use mvn install:install-file to do this. To do this, follow the instructions at https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html .
Hope this helps!
Update: by local repository, I might mean differently than you referred to in your comment -- I mean the local repository on each user's computer, versus a repository server that is local to your intranet. Sorry if that caused any confusion :)
I'm working on an Android project and have some core code(that has some dependencies) that i'd like to version and make into a library/artifact(?) that I can pull into other projects. I'm using Maven to build and my editor is IntelliJ. I've never created a .jar but I think that's what I want to do.
In IntelliJ, i've gone to File->Project Structure->Artifacts->+ but I'm lost. I don't know how to define which source directories and files to include in the jar and I'm unsure if I need to include the actual jars of its dependencies in the jar? or define those dependencies in a pom.xml file and include the pom.xml file in the jar?
Any clarification would be much appreciated.
Maven project's dependencies are defined in its pom.xml file (basically, changing the dependencies from here is what are you visually doing using Intellij, by File->Project Structure->Artifacts). The source files that Maven takes are the ones which are into the src/main directory of your project. The build is done by default excluding the dependencies, so if you want to include them in your final jar, you have to specify this in your pom file (plugins):
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
After that you have to execute mvn install command to have your jar created. You can do it running maven externally or from your IDE (if you have some Maven plugin installed) and the jar will be created in project's target folder.
The settings in IntelliJ IDEA will only create the JAR when you build using the IntelliJ IDEA compiler. To have maven create the JAR and deploy it to the maven repository, you need to use the maven assembly plugin. There is a predefined configuration for creating a jar with dependencies.
The Maven Shade Plugin can also create JARs and handle more advanced use cases. But you'll probably want to start with the assembly plugin/
Something bother me a lot...
On a big project with many dependencies, some of them are set as SNAPSHOT in Maven2.
The matter is that it seems i can't get the sources through Eclipse without loading the project or fixing the dependency to the last release.
For debugging, it's really annoying me...
EDIT
This is what i get in eclipse maven console:
26/08/10 11:31:46 CEST: Downloading http://repo-maven/archiva/repository/snapshots/com/blabla/1.1-SNAPSHOT/blabla-1.1-20100824.213711-80-javadoc.jar
26/08/10 11:31:47 CEST: Could not download sources for com.blabla:blabla:1.1-20100824.213711-80
On archiva i can see the deployed stuff i want to retrieve in eclipse...
Repository snapshots
Group ID com.blabla
Artifact ID blabla
Version 1.1-20100824.213711-80
Packaging jar
Parent com.blabla bla 1.1-SNAPSHOT (View)
Other Versions 1.1-20100824.213535-79
I can download sources of this artifact with my browser but not within Eclipse... Any idea?
The matter is that it seems I can't get the sources through Eclipse without loading the project or fixing the dependency to the last release. For debugging, it's really annoying me...
Well, these modules are probably not publishing source JARs as part of the "regular" build process (i.e. outside the release). If these modules are under your control (which is my understanding), configuring the Maven Source Plugin to produce source JARs for them and deploying them in your corporate repo should solve the problem. From the Usage page:
Installing the sources along with your artifact
There are two ways to do this. You can
either bind this plugin to a phase or
you can add it to a profile. The goals
source:jar-no-fork and
source:test-jar-no-fork are preferred
for binding the goal to the build
lifecycle.
Installing the sources using a phase binding
Here is how you would configure the
plugin in your pom.xml to run
automatically during the verify phase:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
We are using the verify phase here
because it is the phase that comes
before the install phase, thus making
sure that the sources jar has been
created before the install takes
place.
Installing the sources using a profile
If you want to install a jar of your
sources along with your artifact
during the release process, you can
add this to your pom.xml file:
<project>
...
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
...
</project>
Using a profile would probably be a good idea so that building source JARs will only be done by the build running at the CI server level but not on developer machines.