用maven构建完整的应用程序文件夹

mib*_*tec 4 java build maven

大多数java独立应用程序在部署到生产环境后最终都会出现在这样的文件夹中.

myapp  
|->lib (here lay all dependencies)  
|->config (here lay all the config-files) 
|->myapp.bat  
|->myapp.sh  
Run Code Online (Sandbox Code Playgroud)

我想知道maven中是否有任何内容可以为我构建并将其放在tar.gz中.

Java:如何构建基于Maven的项目的独立发行版?别无选择.我不想让maven打开我需要的所有罐子.

yor*_*rkw 6

这种部署目录结构非常流行,并被许多优秀的应用程序采用,如apache maven和ant.

是的,我们可以通过在maven包阶段使用maven-assembly-plugin来实现这一点.

示例pom.xml:

  <!-- Pack executable jar, dependencies and other resource into tar.gz -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-5</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals><goal>attached</goal></goals>
      </execution>
    </executions>
    <configuration>
      <descriptors>
        <descriptor>src/main/assembly/binary-deployment.xml</descriptor>
      </descriptors>
    </configuration>
  </plugin>
Run Code Online (Sandbox Code Playgroud)

示例binary-deployment.xml:

<!--
  release package directory structure:
    *.tar.gz
      conf
        *.xml
        *.properties
      lib
        application jar
        third party jar dependencies
      run.sh
      run.bat
-->
<assembly>
  <id>bin</id>
  <formats>
    <format>tar.gz</format>
  </formats>
  <includeBaseDirectory>true</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>src/main/java</directory>
      <outputDirectory>conf</outputDirectory>
      <includes>
        <include>*.xml</include>
        <include>*.properties</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>src/main/bin</directory>
      <outputDirectory></outputDirectory>
      <filtered>true</filtered>
      <fileMode>755</fileMode>
    </fileSet>
    <fileSet>
      <directory>src/main/doc</directory>
      <outputDirectory>doc</outputDirectory>
      <filtered>true</filtered>
    </fileSet>
  </fileSets>
  <dependencySets>
    <dependencySet>
      <outputDirectory>lib</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>false</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>
Run Code Online (Sandbox Code Playgroud)