29

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

Mohamed
  • 2,342
  • 4
  • 21
  • 32

8 Answers8

34

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.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
  • this is nice but the addition `classes` is unusual these days – aliopi Jun 13 '17 at 07:05
  • 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 https://bugs.eclipse.org/bugs/show_bug.cgi?id=502349 – ojathelonius Mar 18 '19 at 16:10
  • there is a standard way using a configuration parameter 'true' you may use with the WAR plugin See https://maven.apache.org/plugins/maven-war-plugin/faq.html#attached – Jan Rasehorn Dec 13 '21 at 13:29
28

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>
Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
  • It seems that I have to change phase to package. If it is set to compile, there is a error on pom.xml. – Stony Jun 19 '13 at 02:31
  • 1
    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? – Lorenzo Sciuto Jul 15 '16 at 13:39
23

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>
Nilesh
  • 2,089
  • 3
  • 29
  • 53
14

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>
Daniel De León
  • 13,196
  • 5
  • 87
  • 72
  • worked for me. only thing is I replaced p.type with p-type as got errors: unknown lifecycle phase ".type=jar" – SmAxlL Jul 11 '22 at 13:20
6

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.

aliopi
  • 3,682
  • 2
  • 29
  • 24
4

Considering your maven project packaging to war

<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.sample</groupId>
    <artifactId>sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

add the following executions to maven-install-plugin

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5.2</version>
        <executions>
            <execution>
                <phase>package</phase>
                <configuration>
                    <packaging>jar</packaging>
                    <file>${project.build.directory}\${artifactId}-${version}.jar</file>
                </configuration>
                <goals>
                    <goal>install-file</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
Cristian Florescu
  • 1,660
  • 20
  • 24
2

mvn package unless your project's packaging is something besides jar. Then you'd need to add an execution of the jar plugin, as described on the plugin's usage page and as the first answer showed. Better yet, if it's not a jar, divide it into two modules: one that's a jar containing your reusable code, and the other the thing that uses it.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
1

The best way to achieve this is by using Maven assembly plugin by this you can also control the final name of the jar :

Add the following plugin in your pom.xml :

     <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptors>
                    <descriptor>assembly.xml</descriptor>
                </descriptors>
                <finalName>${artifactId}-${version}</finalName>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </plugin>

assembly.xml :

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
  <id>model</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>${project.build.outputDirectory}</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>**/*</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

You can finally build the project with the command below:

mvn clean package assembly:single install

Gaurav
  • 11
  • 1