繁体   English   中英

Maven - 生成Jar和战争

[英]Maven - Generate Jar and War

我有一个CXF WS项目,我会在另一个项目中使用它,我会在Web项目中使用这个WS,但我不知道如何生成Jar文件。

请你有任何想法或一个例子吗?

谢谢

maven-war-plugin支持创建一个仅包含类的单独工件。

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

请参阅'attachClasses'参数。 无需添加jar插件或乱七八糟的包装。 只需将war插件添加到pluginManagement并启用它即可。

但是,我担心这不是你想要的。 要使用CXF Web服务,您需要一个客户端。 要获取客户端,请按照CXF示例中的说明了解如何生成和使用客户端存根。 你需要一个单独的maven项目。

尝试将此添加到您的构建部分:

<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>

将以下内容添加到war项目的pom.xml中。

<attachClasses>true</attachClasses>配置war插件

<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>

将以下内容添加到要导入jar of war项目的项目的pom.xml中

<classifier>classes</classifier>到依赖项导入

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

这是通过财产实现它的一种方法。 默认情况下,它会生成一个war文件,当你想要jar时只需设置属性。

mvn install -Dp.type=jar

的pom.xml

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

这应该工作:

<!-- 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>

摘自此博客

mvn package除非你的项目的包装jar之外的东西。 然后你需要添加一个 jar插件 的执行 ,如插件的使用页面所述 ,并且第一个答案显示。 更好的是,如果它不是一个jar,将它分成两个模块:一个是包含可重用代码的jar,另一个是使用它的东西。

实现这一目标的最佳方法是使用Maven程序集插件,你也可以控制jar的最终名称:

在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>

您最终可以使用以下命令构建项目:

mvn clean package assembly:单个安装

考虑到你的maven项目打包战争

<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>

将以下执行添加到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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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