Maven - Generate Jar and War

后端 未结 8 2033
轻奢々
轻奢々 2020-12-29 04:24

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

相关标签:
8条回答
  • 2020-12-29 04:40

    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>
    
    0 讨论(0)
  • 2020-12-29 04:41

    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.

    0 讨论(0)
  • 2020-12-29 04:44

    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>
    
    0 讨论(0)
  • 2020-12-29 04:44

    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.

    0 讨论(0)
  • 2020-12-29 05:01

    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.

    0 讨论(0)
  • 2020-12-29 05:02

    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>
    
    0 讨论(0)
提交回复
热议问题