Maven - Generate Jar and War

65,214

Solution 1

The maven-war-plugin supports creating a separate artifact that just contains the classes.

http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html

See the 'attachClasses' parameter. No need to add in the jar plugin or mess with the packaging. Just add the war plugin to pluginManagement and turn this on.

However, I fear this this isn't what you want. To consume a CXF web service, you need a client. To get a client, follow the instructions in the CXF samples for how to generate and use client stubs. You'll want a separate maven project for this.

Solution 2

Try adding this into your build section:

<build>
  <plugins>
    <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>
  </plugins>
</build>

Solution 3

Add following to pom.xml of war project.

<attachClasses>true</attachClasses> to configuration of war plugin

<groupId>com.yourorg.foobar</groupId>
<artifactId>hello-world</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>hello</name>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <attachClasses>true</attachClasses>
            </configuration>
        </plugin>
    </plugins>
</build>

Add following to pom.xml of the project where you want to import jar of war project

<classifier>classes</classifier> to dependency import

<dependency>
    <groupId>com.yourorg.foobar</groupId>
    <artifactId>hello-world</artifactId>
    <version>1.0</version>
    <classifier>classes</classifier>
</dependency>

Solution 4

This is a one way to achieve it, via property. By default it will generate a war file, and when you want the jar just set the property.

mvn install -Dp.type=jar

pom.xml

<properties>
   <p.type>war</p.type>
</properties>
<packaging>${p.type}</packaging>

Solution 5

This should work:

<!-- Maven JAR plugin -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>jar-services-provided</id>
            <phase>compile</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- Install the jar locally -->
<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>

Taken from this blog.

Share:
65,214

Related videos on Youtube

Mohamed
Author by

Mohamed

Updated on December 05, 2020

Comments

  • Mohamed
    Mohamed over 3 years

    I have a CXF WS project that I would use it in another project, I would consume this WS in a Web Project but I don't know how to generate Jar file.

    Please have you any idea or an example?

    Thank you

  • Stony
    Stony almost 11 years
    It seems that I have to change phase to package. If it is set to compile, there is a error on pom.xml.
  • Lorenzo Sciuto
    Lorenzo Sciuto almost 8 years
    This worked, but how would you include this *.jar produced by the maven-jar-plugin inside a war package built by the maven-war-plugin?
  • barha
    barha about 7 years
    is there a way to change the jar file's name?
  • aliopi
    aliopi almost 7 years
    this is nice but the addition <classifier>classes</classifier> is unusual these days
  • ojathelonius
    ojathelonius about 5 years
    Do note that an Eclipse bug prevents snapshots from being updated properly. Instead of doing Update project -> Update snapshots/releases, run mvn clean install -U. See bugs.eclipse.org/bugs/show_bug.cgi?id=502349
  • anha1979
    anha1979 over 4 years
    I wanted to generate a war, but just got a .jar instead, but adding <packaging>war</packaging> as you suggested above helps! Thanks
  • Cristian Florescu
    Cristian Florescu over 4 years
    In this case maven-install-plugin create/install also a jar file. The war file is created using maven-war-plugin
  • Jan Rasehorn
    Jan Rasehorn over 2 years
    there is a standard way using a configuration parameter '<archiveClasses>true</archiveClasses>' you may use with the WAR plugin See maven.apache.org/plugins/maven-war-plugin/faq.html#attached