Deploy additional jar file with Maven?

26,710

Solution 1

Maven deploy means deploy artifact to a Maven Repository, not an application server.

Configure additional JAR artifact like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>make-a-jar</id>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Attach this new artifact to your project:

<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>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
                        <!-- <file>${project.build.directory}/${project.build.finalName}.jar</file> - if finalName is defined -->
                        <type>jar</type>                           
                    </artifact>
                </artifacts>
            </configuration>
        </execution>
    </executions>
</plugin>

Solution 2

This blog post and its comments have the answer.

These three plugin configurations will allow you to build/install/deploy a jar version alongside the war.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>make-a-jar</id>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <packaging>jar</packaging>
                <artifactId>${project.artifactId}</artifactId>
                <groupId>${project.groupId}</groupId>
                <version>${project.version}</version>
                <file>
                    ${project.build.directory}/${project.artifactId}-${project.version}.jar
                </file>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <executions>
        <execution>
            <phase>deploy</phase>
            <goals>
                <goal>deploy-file</goal>
            </goals>
            <configuration>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
                <url>${project.distributionManagement.repository.url}</url>
                <artifactId>${project.artifactId}</artifactId>
                <groupId>${project.groupId}</groupId>
                <version>${project.version}</version>
                <file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>

Solution 3

The "maven way" is to split out src/main/java into a separate module, and have the war file depend on that.

If you're absolutely resistant to that approach, you may be able to use a profile to alter the contents of the packaging element. I'm not sure if that's possible though.

Solution 4

Separating them is the right way to go. Forcing maven to produce a war and a jar in the same module is possible but will cause you problems down the road.

Solution 5

You should add the corresponding dependency of the artifact in the dependencies of the pom file.

Ex:

<dependency>
        <groupId>org.apache.myfaces.core</groupId>
        <artifactId>myfaces-api</artifactId>
        <version>1.2.2</version>
        <scope>compile</scope>
</dependency>
Share:
26,710

Related videos on Youtube

Joshua Swink
Author by

Joshua Swink

Updated on July 09, 2022

Comments

  • Joshua Swink
    Joshua Swink almost 2 years

    I have an artifact which is being built and deployed in a particular way (not as a jar file). In the course of deployment, a war file is built.

    How can I configure the pom so that the artifact is also deployed as a jar file, to a different location?

    • Zac Thompson
      Zac Thompson about 15 years
      What is the "particular way" that you are building and deploying? What is meant by "in the course of deployment"? A sample of your current pom might help make this question clearer.
  • Joshua Swink
    Joshua Swink about 15 years
    Make an artifact dependent on itself? I don't understand how this would affect deployment. And in fact maven doesn't like the resulting cyclic reference.
  • Andrea Asr Franchi
    Andrea Asr Franchi almost 11 years
    With "build-helper-maven-plugin", I was able to deploy war, jar and test-jar from a single project. Unfortunately, creating new modules was not an option, for reasons other then technical. This was a life saver!
  • Danubian Sailor
    Danubian Sailor over 10 years
    You can't change packaging in profile directly, but you can make packaging dependent on property and alter that property in profile.
  • Amin
    Amin over 8 years
    Using the maven-install-plugin as you described has solved the same issue I've encountered while trying to install the generated but not deployed jar file using android maven plugin with packaging set to aar. I need the jar file as a workaround for netbeans IDE visual issue with such dependencies.
  • smaudet
    smaudet about 7 years
    Should be self evident, but remember if your project created the jar with a different name (or you set finalName on your build), you will need to adjust accordingly to ${build.finalName}.jar instead of the above. Check your target directory to see what jar is actually produced.
  • smaudet
    smaudet about 7 years
    Which problems? If you need a jar, and it is produced by that module...then you should be fine as long as that jar is always produced by that module... (and you have dependency on that jar). If there's a more specific maven problem you had in mind...
  • Laloi
    Laloi about 3 years
    This solution solved my problem, but had to add "<?m2e ignore?>" in maven-jar-plugin / executions / execution, to prevent an error from appearing in Eclipse (Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-jar-plugin:3.2.0:jar).
  • Laloi
    Laloi about 3 years
    (my problem which was to generate and deploy both jar and war with Maven)
  • Laloi
    Laloi about 3 years
    To complete, you shall replace "<file>some file</file>" by "<file>${project.build.directory}/${project.artifactId}-${pr‌​oject.version}.jar</‌​file>" (or "<file>${project.build.directory}/${project.build.finalName}‌​.jar</file>" if you defined a specific final name).