我可以拥有什么作为Maven工件,我该如何使用它?

时间:2014-08-04 06:28:10

标签: java maven build

我知道这似乎是一个已被问到的问题,但我找不到能给你带来很多帮助的答案。

我多次读过,每个项目最好生成一个工件。但我的问题是:作为Maven神器,我能拥有什么?

例如:我有一个定制包装" MyPack"使用自定义生命周期,我可以拥有整个目录" Mydirectory"作为人工制品?

让我们说目录的结构是这样的:

mydirectory中
----- | --jars
----- | ----- | --- client.jar
----- | ----- | --- server.jar
----- | --jsps
----- | ----- | --- page1.jsp
----- | ----- | --- page2.jsp
----- | --imgs
----- | ----- | --- img1.png
----- | ----- | --- img2.png

然后,我想创建一个包含" MyPack"和#34; MyPack"类型的依赖关系。在这个项目中,我的Java类需要在Maven存储库中编译的client.jar和server.jar,我想在我的新项目中从存储库中复制所有jsps和imgs。

我可以使用自定义Maven插件完成所有这些吗?

1 个答案:

答案 0 :(得分:0)

你可以拥有任何你选择作为神器的maven神器。 使用maven assembly plugin生成工件:

    <build>
       <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <descriptors>
                    <descriptor>${basedir}/src/main/assembly/shared.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
     ....
      </plugins> 
     ....
  </build>      

在shared.xml文件中,指定要包含在工件中的格式和文件:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>shared</id>
<formats>
    <format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
    <fileSet>
        <outputDirectory>/</outputDirectory>                            
        <directory>${basedir}/.....</directory>
        <includes>
            <include> ... </include>
        </includes>
        <fileMode>0644</fileMode>
        <filtered>false</filtered>
        <lineEnding>unix</lineEnding>
    </fileSet>
  </fileSets>
</assembly>

当您执行“maven deploy”时,“共享”工件将与项目的主工件一起打包和部署。 要在另一个项目中使用工件,请使用目标为“unpack-dependencies”的maven-dependency-plugin