Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Attaching multiple artifacts

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.

like image 873
Swaranga Sarma Avatar asked Nov 23 '11 09:11

Swaranga Sarma


People also ask

How many Maven artifacts should I build?

My suggestion is to instead let Maven build one artifact for each configuration and label them accordingly, so when looking at a specific version in the Maven repository you find, one development artifact, one test artifact and one production artifact, each clearly labeled. Then you never run into the problems listed above.

What is a Maven artifact classifier?

What Is a Maven Artifact Classifier? A Maven artifact classifier is an optional and arbitrary string that gets appended to the generated artifact's name just after its version number. It distinguishes the artifacts built from the same POM but differing in content.

What is “sources” in Maven?

This is used for instance by the source plugin that creates a source jar file for a Maven project, resulting in a file with the name <artifact id>-<version>-sources.jar. The classifier for this file is “sources”.

What does <version> mean in an artifact name?

This is a suffix that is appended to the name of the artifact to distinguish it from the main artifact. This is used for instance by the source plugin that creates a source jar file for a Maven project, resulting in a file with the name <artifact id>-<version>-sources.jar.


2 Answers

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.

like image 86
Jörn Horstmann Avatar answered Oct 06 '22 00:10

Jörn Horstmann


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>
like image 34
Raghuram Avatar answered Oct 05 '22 23:10

Raghuram