发布于 

常用maven打包配置记录

记录常用的一些maven打包配置。

  1. 打成jar包,并放入指定目录:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
    <outputDirectory>${project.build.directory}/${project.artifactId}/</outputDirectory>
    </configuration>
    </plugin>
    </plugins>
    </build>
  2. 将依赖包拷贝入指定文件夹:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
    <execution>
    <id>copy-dependencies</id>
    <phase>prepare-package</phase>
    <goals>
    <goal>copy-dependencies</goal>
    </goals>
    <configuration>
    <outputDirectory>${project.build.directory}/${project.artifactId}/</outputDirectory>
    <overWriteReleases>false</overWriteReleases>
    <overWriteSnapshots>false</overWriteSnapshots>
    <overWriteIfNewer>true</overWriteIfNewer>
    <includeArtifactIds>*</includeArtifactIds>
    <excludeArtifactIds>isuwang-soa-core</excludeArtifactIds>
    </configuration>
    </execution>
    </executions>
    </plugin>

    这里<includeArtifactIds>表示要包括的包,<excludeAritifactIds>表示排除的包,冲突的情况下以<excludeArtifactIds>为准。

  3. 将资源文件移动到指定目录:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <artifactId>isuwang-soa-container</artifactId>
    <properties>
    <devName>${project.artifactId}</devName>
    </properties>
    <build>
    <resources>
    <resource>
    <filtering>true</filtering>
    <directory>${basedir}/src/main/command</directory>
    <includes>
    <include>startup.sh</include>
    <include>shutdown.sh</include>
    </includes>
    <targetPath>${project.build.directory}/${devName}/bin</targetPath>
    </resource>
    <resource>
    <filtering>true</filtering>
    <directory>${basedir}/src/main/resources</directory>
    <includes>
    <include>containers.xml</include>
    <include>logback.xml</include>
    <include>filters-server.xml</include>
    </includes>
    <targetPath>${project.build.directory}/${devName}/conf</targetPath>
    </resource>
    </resources>
    </build>
  4. 创建空文件夹:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
    <execution>
    <id>create-empty-directory</id>
    <phase>process-classes</phase>
    <goals>
    <goal>run</goal>
    </goals>
    <configuration>
    <tasks>
    <mkdir dir="${project.build.directory}/${devName}/apps"/>
    <mkdir dir="${project.build.directory}/${devName}/logs"/>
    </tasks>
    </configuration>
    </execution>
    </executions>
    </plugin>

    因为maven本身不具备创建空文件夹能力(打包的时候,如果是空文件夹会被忽略),所以要用这个ant插件。

  5. 使用profile配置不同条件下打包:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    <profiles>
    <profile>
    <id>default</id>
    <activation>
    <activeByDefault>true</activeByDefault>
    </activation>
    </profile>

    <profile>
    <id>dev</id>
    <activation>
    <activeByDefault>false</activeByDefault>
    </activation>

    <build>
    ...
    </build>
    </profile>
    </profiles>

    打包时clean install默认使用default,但如果用clean install -Pdev加入参数,就使用dev下的配置。

  6. 打包源文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
       <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>2.4</version>
    <executions>
    <execution>
    <id>attach-sources</id>
    <phase>verify</phase>
    <goals>
    <goal>jar-no-fork</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
  7. 编译scala

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
      <plugin>
    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
    <execution>
    <id>scala-compile-first</id>
    <phase>process-resources</phase>
    <goals>
    <goal>add-source</goal>
    <goal>compile</goal>
    </goals>
    </execution>
    <execution>
    <id>scala-test-compile</id>
    <phase>process-test-resources</phase>
    <goals>
    <goal>testCompile</goal>
    </goals>
    </execution>
    </executions>
    <configuration>
    <scalaVersion>${scala.version}</scalaVersion>
    </configuration>
    </plugin>

    不这样做install的时候,java代码可能提示找不到scala的包

  8. 指定多个源文件目录

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
      <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
    <execution>
    <id>add-source</id>
    <phase>generate-sources</phase>
    <goals>
    <goal>add-source</goal>
    </goals>
    <configuration>
    <sources>
    <source>${basedir}/src/main/java</source>
    <source>${basedir}/src/main/scala</source>
    </sources>
    </configuration>
    </execution>
    <execution>
    <id>add-test-source</id>
    <phase>generate-sources</phase>
    <goals>
    <goal>add-test-source</goal>
    </goals>
    <configuration>
    <sources>
    <source>src/test/java</source>
    </sources>
    </configuration>
    </execution>
    </executions>
    </plugin>

    Maven默认只允许指定一个主Java代码目录和一个测试Java代码目录,虽然这其实是个应当尽量遵守的约定,但偶尔你还是会希望能够指定多个源码目录(例如为了应对遗留项目),build-helper-maven-plugin的add-source目标就是服务于这个目的,通常它被绑定到默认生命周期的generate-sources阶段以添加额外的源码目录。需要强调的是,这种做法还是不推荐的,因为它破坏了 Maven的约定,而且可能会遇到其他严格遵守约定的插件工具无法正确识别额外的源码目录。

  9. 将依赖打入jar包,并指明可执行入口类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
      <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
    <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
    <manifest>
    <addClasspath>true</addClasspath>
    <classpathPrefix></classpathPrefix>
    <mainClass>com.isuwang.soa.code.Scrooge</mainClass>
    </manifest>
    </archive>
    </configuration>
    <executions>
    <execution>
    <id>make-assembly</id>
    <phase>package</phase>
    <goals>
    <goal>single</goal>
    </goals>
    </execution>
    </executions>
    </plugin>