简体   繁体   中英

Maven - Generate Jar and War

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

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.

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>

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>

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>

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 .

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.

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM