maven打包相关插件总结(非web服务器运行环境)

Beaver_ 发布于 2015/11/23 21:58
阅读 1K+
收藏 3

Linux基金会免费官方培训及考试申请即将截止,戳这里申请!>>>

maven打包相关插件总结(非web服务器运行环境)







有时候,我们需要把一个maven项目打包成一个jar,并且把依赖的lib都打包到一个lib目录下,利用
java -Dxxx(设置环境变量等) -classpath(类查询路径设置)  ./lib/* ...    Main (程序入口)
来运行我们的程序(不是我们熟悉的web服务)。


一、项目资源文件打包设置
一般maven resources 目录下的资源文件会一起打包到jar包中,但是我们的配置文件必须能够配置,所以,我们在处理这些
要配置的文件时候,我们不能把这些文件打包到jar包内。


Apache Maven Resources Plugin


The Resources Plugin handles the copying of project resources to the output directory. 
There are two different kinds of resources: main resources and test resources. 
The difference is that the main resources are the resources associated to the main source code 
while the test resources are associated to the test source code。


参考:

http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html


我们很容易定制src/main/resources 等其他资源路径的过滤设置。比如:
<project>
  ...
  <build>
    ...
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <excludes>
          <exclude>**/*.xml</exclude>
        </excludes>
      </resource>
      ...
    </resources>
    ...
  </build>
  ...
</project>
这样,我们就可以设置那些配置文件打包到jar中,哪些不打包。


二、项目打包成一个压缩包文件,比如tar.gz,zip等。
maven-Assembly-plugin 插件。


The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.


Your project can build distribution "assemblies" easily, using one of the convenient, prefabricated assembly descriptors. These descriptors handle many common operations, such as packaging a project's artifact along with generated documentation into a single zip archive. Alternatively, your project can provide its own descriptor and assume a much higher level of control over how dependencies, modules, file-sets, and individual files are packaged in the assembly.


Currently it can create distributions in the following formats:


zip
tar
tar.gz (or tgz)
tar.bz2 (or tbz2)
jar
dir
war
and any other format that the ArchiveManager has been configured for
If your project wants to package your artifact in an uber-jar, the assembly plugin provides only basic support. For more control, use the Maven Shade Plugin.


To use the Assembly Plugin in Maven, you simply need to:


choose or write the assembly descriptor to use,
configure the Assembly Plugin in your project's pom.xml, and
run "mvn assembly:single" on your project.
To write your own custom assembly, you will need to refer to the Assembly Descriptor Format reference.




官方文档已经给了很好的例子 allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive。


<project>
  [...]
  <build>
    [...]
    <plugins>
       <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
       </plugin>


    <plugins>
      [...]
</project>






src/main/assembly/assembly.xml 文件内容即:aven Assembly Plugin relies on the provided assembly descriptors to dictate its execution.

参考:

http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html



式例:
<assembly>
    <id>bin</id>
    <formats>
        <format>tar.gz</format>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>src/main/resources/bin</directory>
            <outputDirectory>/bin</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/config</outputDirectory>
            <includes>
                <include>xxx.properties</include>
                <include>common.properties</include>
                <include>jdbc.properties</include>
                <include>logback.xml</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>  


其它看官方文档吧。。


三、发布私服


最好,到包好了之后, 发现发布到私服的时候,有错误:
ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project payment: Failed to deploy artifacts: Could not transfer artifact Xxxxxxx:tar.gz:bin:0.0.2 from/to xxxx-release (http:/xxx/releases): Failed to transfer file: http://xxxx/repositories/releases/xxx/xxx/0.0.2/xxx-0.0.2-bin.tar.gz. Return code is: 400, ReasonPhrase: Bad Request. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Finished: FAILURE


寻找解决途径:
http://stackoverflow.com/questions/2244344/deploying-assembly-package-with-maven-release-plugin


http://www.mojohaus.org/build-helper-maven-plugin/    
build-helper:attach-artifact Attach additional artifacts to be installed and deployed.




http://www.mojohaus.org/build-helper-maven-plugin/usage.html


Attach additional artifacts to your project


示例:


 <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-distribution</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>target/${pom.artifactId}-${pom.version}-bin.tar.gz</file>
                                    <type>tar.gz</type>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


具体配置还要根据需求,仔细查看相关文档。


来自csdn博客:http://blog.csdn.net/doctor_who2004/article/details/50001137

加载中
返回顶部
顶部