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