Maven: Attaching multiple artifacts - java

I have a maven project that uses some custom jars [not found in any repository]. To add them along with the Maven build I am using the attach-artifact goal in maven. Following is my pom file:
<?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>
<groupId>com.maximus</groupId>
<artifactId>adminbuild</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>adminbuild</name>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<outputDirectory>target</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${basedir}/../../resources/dependencies/java/customjar1.jar</file>
<type>jar</type>
</artifact>
<artifact>
<file>${basedir}/../../resources/dependencies/java/customjar2.jar</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
My projects that use these jars [customjar1.jar, customjar2.jar] have their dependency on the above pom file [adminbuild].
When I execute the mvn clean install command, I get the following error:
[ERROR] Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.7:attach-artifact (attach-artifacts) on project adminbuild: The artifact with same type and classifier: jar:null is used more than once. -> [Help 1]
Following is the output of mvn -version command:
Apache Maven 3.0.3 (r1075438; 2011-02-28 23:01:09+0530)
Maven home: C:\maven
Java version: 1.6.0_26, vendor: Sun Microsystems Inc.
Java home: C:\Java\jdk1.6.0_26\jre
Default locale: en_IN, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
It seems that the way I am attaching the artifacts is incorrect. Should I not attach multiple artifacts in the same pom file? If yes then how. Please help.

Attached artifacts are normally used to install additional files created by your build, like the classes jar of a webapp, documentation or sources.
To add files to your maven repository so they are available as dependencies you should use the install-file goal.
Edit: Attached artifacts are identified by the same groupId and artifactId as your main project but with a different classifier. In your configuration, you did not specify the classifier, hence the error message.

You would want to have these custom jars in a repository manager like nexus so that they can be downloaded/used like a normal dependency jar.
Assuming that is not possible and seeing that these jars are in well-known location, perhaps you could specify these custom jars with system scope in the projects that need them?
<project>
...
<dependencies>
<dependency>
<groupId>my-group</groupId>
<artifactId>customjar1</artifactId>
<version>a.b.c</version>
<scope>system</scope>
<systemPath>${basedir}/../../resources/dependencies/java/customjar1.jar</systemPath>
</dependency>
</dependencies>
...
</project>

I used a secondary project to do this along with a multi-module.
https://stackoverflow.com/q/7908872/242042

Related

How to build nifi processor nar file and dependencies from maven central

I am trying to build a nar file from maven repo central. I am not very used to maven, so I will explain the steps I followed until the blocking point where I am stuck now.
I want to generate the nar files for this artifact:
https://mvnrepository.com/artifact/org.apache.nifi/nifi-hwx-schema-registry-nar/1.10.0
So I created this pom.xml file:
<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>
<groupId>TestMaven</groupId>
<artifactId>TestMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-hwx-schema-registry-nar</artifactId>
<version>1.10.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<artifactItems>
<artifactItem>
<!-- hardcode values, or use properties, depending on what you want
to do -->
<groupId>TestMaven</groupId>
<artifactId>TestMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>[ packaging ]</type>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</artifactItem>
</artifactItems>
<!-- other configurations here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And then I try to compile whit this commands (first commands are to include maven dependencies on the PATH):
export JAVA_HOME=/usr/jdk64/jdk1.8.0_112
export M2_HOME=/usr/local/apache-maven
export M2=$M2_HOME/bin export PATH=$M2:$PATH
mvn -U -X dependency:copy-dependencies -DskipTests
-Dclassifier=sources -DoutputDirectory=target -Dhttp.proxyHost=X.X.X.X -Dhttp.proxyPort=80 -Dhttps.proxyHost=X.X.X.X -Dhttps.proxyPort=80
And I am getting this error, which says that the maven dependency couldn't be found:
[ERROR] Failed to execute goal on project TestMaven: Could not resolve
dependencies for project TestMaven:TestMaven:jar:0.0.1-SNAPSHOT: Could
not find artifact
org.apache.nifi:nifi-hwx-schema-registry-nar:jar:1.10.0 in central
(https://repo.maven.apache.org/maven2) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to
execute goal on project TestMaven: Could not resolve dependencies for
project TestMaven:TestMaven:jar:0.0.1-SNAPSHOT: Could not find
artifact org.apache.nifi:nifi-hwx-schema-registry-nar:jar:1.10.0 in
central (https://repo.maven.apache.org/maven2)
Thank you
You would need to get the source code for Apache NiFi version 1.10.0 and then build that module.
You could get the code by cloning the git repo and checking out the tag rel/1.10.0.
https://github.com/apache/nifi/tree/rel/nifi-1.10.0/nifi-nar-bundles/nifi-standard-services/nifi-hwx-schema-registry-bundle
Then run mvn clean package from the location above.

installing a jar with maven-install-plugin to local repo creates pom without dependencies [duplicate]

I have a maven plugin, which I have not uploaded to the central repository, but which I want to use in my projects.
What works
I can install the maven plugin like this:
git clone https://github.com/RudolfVonKrugstein/jinja-maven-plugin.git
cd jinja-maven-plugin
mvn install
Then I can use the plugin like this pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>de.wintercloud</groupId>
<artifactId>sample</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>de.wintercloud</groupId>
<artifactId>jinja-maven-plugin</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>de.wintercloud</groupId>
<artifactId>jinja-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>renderjinja</goal>
</goals>
</execution>
</executions>
<configuration>
<outputFile>out.txt</outputFile>
<templateFile>templ.jinja</templateFile>
<varFile>vars.yaml</varFile>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here are the other files relevant to the compile:
templ.jinja:
{{ Name }}
vars.yaml:
Name: MyWonderfullName
This works:
> mvn compile
> cat out.txt
MyName
Nice!
What does not work
Now I am trying to give the plugin as a jar to my colleagues so that they can simple install the jar. The Idea is to do it like this:
git clone https://github.com/RudolfVonKrugstein/jinja-maven-plugin.git
cd jinja-maven-plugin
mvn package
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=target/jinja-maven-plugin-1.0.jar
When I now do (in the sample project dir)
mvn compile
I get this error:
[ERROR] Failed to execute goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja (default) on project sample: Execution default of goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja failed: A required class was missing while executing de.wintercloud:jinja-maven-plugin:1.0:renderjinja: org/yaml/snakeyaml/Yaml
How can I install the jar so that I can use it as a plugin?
It looks to me as if dependencies are missing. Why?
You just hit MINSTALL-110, which is going to be fixed in the next 3.0.0 release of the Maven Install Plugin. The core issue here is that you're installing manually a JAR file with the file parameter, the plugin detects that there is a POM inside, but only keeps the coordinate information (group id, artifact id, packaging and version), not the whole POM. As such, when the POM is installed, the dependencies aren't kept.
Indeed, if you take a look at the installed POM, you will see
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>de.wintercloud</groupId>
<artifactId>jinja-maven-plugin</artifactId>
<version>1.0</version>
<packaging>maven-plugin</packaging>
<description>POM was created from install:install-file</description>
</project>
which shows that the dependencies weren't retained. And if you take a look at the Maven logs in debug mode when running the install-file command, it will show
[DEBUG] Using META-INF/maven/de.wintercloud/jinja-maven-plugin/pom.xml for groupId, artifactId, packaging and version
A work-around waiting for version 3.0.0 is to specify the path to the POM file to install, along with the main artifact, by specifying the pomFile parameter. Run the following instead:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=target/jinja-maven-plugin-1.0.jar -DpomFile=pom.xml
Then the full POM will be installed, not a generic stub.
With this change, the error
[ERROR] Failed to execute goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja (default) on project sample: Execution default of goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja failed: A required class was missing while executing de.wintercloud:jinja-maven-plugin:1.0:renderjinja: org/yaml/snakeyaml/Yaml
will not happen anymore. Maven will correctly download the dependencies and use them for the plugin.

Migrate dependencies from local to Maven

I have a Java code in which I added JARs using Maven and also from the local system. Now as I am running it on a different system, the local JARs are missing. How can I add everything to Maven and remove the local class paths?
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.memeanalytics</groupId>
<artifactId>kafka-consumer-storm</artifactId>
<packaging>jar</packaging>
<version>1.1.0</version>
<name>HibernateExample</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>github-releases</id>
<url>http://oss.sonatype.org/content/repositories/github-releases/</url>
</repository>
<repository>
<id>clojars</id>
<url>http://clojars.org/repo</url>
</repository>
<repository>
<id>cloudera-repo-releases</id>
<url>https://repository.cloudera.com/artifactory/repo/</url>
</repository>
</repositories>
<dependencies>
<!-- <dependency> <groupId>net.wurstmeister.storm</groupId> <artifactId>storm-kafka-0.8-plus</artifactId>
<version>0.4.0</version> </dependency> -->
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
<version>1.1.1.3</version> <!-- was 1.1.0-M3 -->
</dependency>
<dependency>
<groupId>org.apache.storm</groupId>
<artifactId>storm-kafka</artifactId>
<version>0.9.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.sd.dwh.kafka.storm.Invoker</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I don't know what you mean by "local JARs". If you mean a mixture of Maven dependencies and manually added JARs into the IDE, then this is a very bad idea. Either you use Maven or not. If you use Maven, everything should be a Maven dependency. In future, you need every manually added jars to be managed by Maven by adding it into the local repository:
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
After you added it into the local repository, you can add it as dependency in your pom.xml as you do with all other dependencies from Maven central.
If you share the project with other people or other computers, you run into the problem you were talking about. In a team (or inside a local network), you usually run a repository manager like Nexus. When there are new dependencies which were manually added (and installed to the local repository) you usually deploy it to Nexus which acts as a proxy between your local network and Maven central. Then, everybody in your team or local network can find these dependencies.
The easiest attempt to fix is to:
1. Take note of all the jars/dependencies that you added manually to the classpath (using your IDE or otherwise)
2. Use maven central search facilities (http://search.maven.org/) to locate those dependencies (one by one, of course) in public repositories. The results you find using online maven searches will include a snippet of the pom for those dependencies (<dependency>...</dependency>). Copy and paste them into your existing pom.xml's dependencies section.
3. Remove your IDE-based classpath entries.
Of course, this can only work if those dependencies are in public repositories.
You can setup a maven repository manager like nexus for your local development which can proxy the maven central repository and you can deploy your own library in it. And when you need to develop on another environment, you can get the dependencies from your local maven repository.

NoClassDefFoundError when using executions in maven

Update
Based on the comments of others and my own tests, this appears to only be a problem with Maven 3.1.1 - I've logged a bug with codehaus
I've created the smallest maven POM file I can to highlight the problem (see below).
I'm using an email server plugin (for integration testing). I can launch the plugin no problem:
mvn emailserver:run
[INFO] --- emailserver-maven-plugin:1.1.1:run (default-cli) # project ---
[INFO] Starting Greenmail mail server...
[INFO] Started Greenmail mail server
[INFO] Waiting for command from client
However, I have configured the plugin to run before the integration-test lifecycle. When run this way, I get a NoClassDefFoundError:
mvn integration-test
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: com/icegreen/greenmail/util/ServerSetup
When I execute maven with -X to see what's happening, in both cases it shows the same dependency tree as far as I can see, so I'm at a bit of a loss as to why I'm getting an exception which it's launched through lifecycle phases. Hopefully someone can help!
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mytest</groupId>
<artifactId>project</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>Test Project</name>
<build>
<plugins>
<plugin>
<groupId>com.btmatthews.maven.plugins</groupId>
<artifactId>emailserver-maven-plugin</artifactId>
<configuration>
<monitorKey>emailserver</monitorKey>
<monitorPort>10025</monitorPort>
<serverName>greenmail</serverName>
<portOffset>13000</portOffset>
<useSSL>false</useSSL>
</configuration>
<executions>
<execution>
<id>run-mail</id>
<goals>
<goal>run</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-mail</id>
<goals>
<goal>stop</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Maven information:
> mvn -v
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 16:22:22+0100)
Maven home: /opt/apache-maven
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: /opt/jdk1.7.0_45/jre
Default locale: en_IE, platform encoding: UTF-8
OS name: "linux", version: "3.11.0-13-generic", arch: "amd64", family: "unix"
NoClassDefFoundError means that the class is present in the classpath at Compile time, but it doesn't exist in the classpath at Runtime.
So clean the project through eclipse, .class files are again will regenerate. Run the maven project again with help of Maven

Maven generates duplicate pom.xml and pom.properties files in a jar

I package my Maven-based Spring app with:
mvn install-DskipTests -Peverything.
And something strange arises. In META-INF of generated jar, I find duplicate pom.xml and pom.properties files. Can someone explain it? Thanks.
Here is extracted pom.xml
<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>
<groupId>com.abc.xyz</groupId>
<artifactId>migrate-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>migrate-app</name>
<url>http://maven.apache.org</url>
<dependencies>
..
</dependencies>
<profiles>
<profile>
<id>everything</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Output for command:
mvn -version
Apache Maven 3.0.4 (r1232337; 2012-01-17 15:44:56+0700)
Maven home: C:\apache-maven-3.0.4\bin..
Java version: 1.6.0_30, vendor: Sun Microsystems Inc.
Java home: C:\Program Files (x86)\Java\jdk1.6.0_30\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
Add
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
</plugins>
</build>
to your pom.xml
It seems like this is a somewhat common problem that arises from a conflict in Eclipse, especially with the m2e-wtp integration.
Some people are indicating that it can be fixed by including clean in the list of goals to run, e.g. mvn clean package.
Also try to update your m2e installation and make sure you don't have multiple versions of m2e or m2eclipse (the pre-Indigo version) installed.
Some references to other people facing this problem:
http://dev.eclipse.org/mhonarc/lists/m2e-users/msg01995.html
https://issues.sonatype.org/browse/MECLIPSEWTP-209
https://issuetracker.springsource.com/browse/STS-2349
https://github.com/sonatype/m2eclipse-extras/issues/9
I know this issue is a bit old, but I thought I'd share an obscure bit of information on this subject. I was running across the same issue, and I found that when I ran maven with Eclipse open, AND my workspace preferences were set to refresh the workspace using native hooks or polling, the extra pom.xml and pom.properties files were added by eclipse! Closing eclipse or removing the refresh workspace option solved the problem for me. This was a hard one to track down, so hopefully someone else will benefit from this.
Follow up to #Dan's work around:
I tried your solution and it appears to work -- at first blush. However, it's just delaying the eventual failure.
It appears than anytime the workspace is refreshed the pom.xml and pom.properties files are created in the /classes directory. After that any later "package" goal will include the duplicate files in the .jar and thus break any later signing.
If the polling is on - you "fail-fast". If it's off you can fail at arbitrary times in future builds.
You need to do "mvn clean" and the do "mvn package" after that or just do "mvn clean package". if "mvn clean" fails due to any reason and if you do "mvn package" after that , you will see this issue again.
Not sure what you mean by duplicate but the jar created by maven does come with pom.xml and pom.properties in META-INF folder. See this section of the maven getting started guide.

Categories

Resources