2012-06-02 61 views
16

我有一个CXF WS项目,我将在另一个项目中使用它,我将在Web项目中使用此WS,但我不知道如何生成Jar文件。Maven - 生成Jar和War

请问你有什么想法或例子吗?

谢谢

回答

26

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

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

见 'attachClasses' 参数。无需添加jar插件或与包装混淆。只需将war插件添加到pluginManagement并将其打开即可。

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

+0

这是不错的,但加上''这些日子是不寻常的 – aliopi

20

尝试添加该到编译部分:

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

看来我不得不改变相位包装。如果它被设置为编译,则在pom.xml上有错误。 – Stony

+0

这有效,但是如何将maven-jar-plugin生成的* .jar包含在由maven-war-plugin构建的war包中? –

6

这是通过属性实现它的一种方式。 默认情况下,它会生成一个war文件,当你想让jar刚设置属性。

mvn install -Dp.type=jar 

的pom.xml

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

添加下列战争项目的pom.xml。

<attachClasses>true</attachClasses>战争插件

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

添加的配置以下到您想要导入战项目的罐子项目的pom.xml

<classifier>classes</classifier>进行依赖进口

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

有没有办法改变jar文件的名字? – barha

+1

这里是答案,http://stackoverflow.com/a/14490656/4417818 – Nilesh

3

这应该工作:

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

摘自this blog

0

实现这一目标的最佳方式是使用Maven Assembly插件通过这个,你还可以控制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> 

部件。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清洁套装组件:一个安装

相关问题