maven assembly打tar.gz包。

背景

项目中maven工程,非jave web项目,之前一直通过eclipse运行。代码调试通过后,要部署到linux服务器上。简单的方法就是直接打成jar包,通过java命令运行。但有一个问题,当涉及到配置文件变更,或者某个java类代码变动的时候,要重新打jar包,再上传,运行。

而我的需求是,将工程打成tar.gz包,在linux服务器上,解压后,通过sh启动脚本运行。配置文件和class文件可以直接修改或者替换,而不需要更换整个jar包。

经过调研,发现maven的assembly插件可以达到此要求。记录一下解决方法。

assembly

介绍

assembly可以定义任何一个文件或者目录归档方式。举个例子,如果的你的Maven 2工程包含”src/main/bin”这个目录,你可以指示assembly插件复制“src/main/bin”目录下所有的文件到bin目录里(归档文件里的目录),并且可以修改它们的权限属性(UNIX mode)。

配置

要使用assembly,首先在pom.xml中,配置assembly-plugin:

    <build>
        <plugins>

            <plugin>
                <artifactId> maven-assembly-plugin artifactId>   
                <configuration>
                     <descriptors>   
                        
                        <descriptor>assembly.xmldescriptor>  
                    descriptors> 
                    <archive>
                        <manifest>
                            <mainClass>com.cmss.clm.AlarmReportmainClass>
                        manifest>
                    archive>
                configuration>
                <executions>
                    <execution>
                         
                        <id>make-assemblyid>
                        
                        <phase>packagephase>
                        <goals>
                             
                            <goal>singlegoal>
                        goals>
                    execution>
                executions>
            plugin>

        plugins>
    build>

maven打成的包是什么样子的,完全由assembly.xml决定。

看下我最终的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>releaseid>
    <formats>
        <format>tar.gzformat
    formats>
    <includeBaseDirectory>falseincludeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>falseunpack>
            <scope>runtimescope>
            <outputDirectory>AlarmReport/liboutputDirectory>
            <useProjectArtifact>falseuseProjectArtifact>
        dependencySet>
    dependencySets>
    <fileSets>
        <fileSet>
            <directory>target/classesdirectory>
            <outputDirectory>AlarmReportoutputDirectory>
        fileSet>
    fileSets>
     <files>    
         <file>  
            <source>startup.shsource>
            <outputDirectory>AlarmReport/binoutputDirectory>  
            <filtered>truefiltered>  
        file>   
    files>  
assembly>
  • release,release会附加在版本后面;
  • tar.gz指明包的格式为tar.gz;
  • false,如果为false,不会额外产生根目录,否则,在tar.gz包中会产生以pom.xml中artifactIdversion命名的根目录。
  • ...是对依赖包的设置:
    • 定义了是否解压依赖包,如果为true,会解压出依赖包中的class文件,反之,则不进行解压;
    • 限定了对哪些依赖包进行操作;(依赖包scope的值是在pom.xml中定义的)
    • 依赖包在tar.gz包中相对于根目录的路径
    • 依赖包中是否包含当前工程;
      assembly中dependencySets的意思就是,将scope为runtime的依赖包,放到AlarmReport/lib目录下。
  • 指定哪些文件包含在打出的tar.gz包中:
    • 指定目录下的文件打包到目录下;
    • 指定文件在tar.gz包中的路径:
      assembly中两个fileSets的作用是,将target/classes下的文件,打包到AlarmReport目录;将startup.sh打包到AlarmReport/bin目录

示例

pom.xml

<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.0modelVersion>

    <groupId>com.cmss.clmgroupId>
    <artifactId>AlarmReportartifactId>
    <version>1.0version>
    <packaging>jarpackaging>
    <name>AlarmReportname>
    <url>http://maven.apache.orgurl>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <build>
        <plugins>
            <plugin>
                <artifactId> maven-assembly-plugin artifactId>   
                <configuration>
                     <descriptors>   
                        
                        <descriptor>assembly.xmldescriptor>  
                    descriptors> 
                    <archive>
                        <manifest>
                            <mainClass>com.cmss.clm.AlarmReportmainClass>
                        manifest>
                    archive>
                configuration>
                <executions>
                    <execution>
                         
                        <id>make-assemblyid>
                        
                        <phase>packagephase>
                        <goals>
                             
                            <goal>singlegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>

    <dependencies>
        ...
    dependencies>
project>

工程目录
maven assembly打tar.gz包。_第1张图片

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>RELEASEid>
    <formats>
        <format>tar.gzformat>
    formats>
    <includeBaseDirectory>falseincludeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>falseunpack>
            <scope>runtimescope>
            <outputDirectory>AlarmReport/liboutputDirectory>
            <useProjectArtifact>falseuseProjectArtifact>
        dependencySet>
    dependencySets>
    <fileSets>
        <fileSet>
            <directory>target/classesdirectory>
            <outputDirectory>AlarmReportoutputDirectory>
        fileSet>
    fileSets>
     <files>    
         <file>  
            <source>startup.shsource>
            <outputDirectory>AlarmReport/binoutputDirectory>  
            <filtered>truefiltered>  
        file>   
    files>  
assembly>

在eclipse中,选择maven-build:
maven assembly打tar.gz包。_第2张图片
在弹出窗口中,输入package:
maven assembly打tar.gz包。_第3张图片
打出的包,名字为AlarmReport-1.0-RELEASE.tar.gz,目录结构如下:

maven assembly打tar.gz包。_第4张图片

TIPS:
如果仅仅想在打包的同时包含有依赖jar包,可以将上面配置文件中的

<descriptors>   
     
     <descriptor>assembly.xmldescriptor>  
descriptors> 

替换成

<descriptorRefs>  
     <descriptorRef>jar-with-dependenciesdescriptorRef>  
descriptorRefs> 

这个jar-with-dependencies是assembly预先写好的一个assembly.xml,就不需要我们自己再写了。

你可能感兴趣的:(maven)