1. 程式人生 > >Maven打包可執行jar並且壓縮

Maven打包可執行jar並且壓縮

1. pom.xml

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.4</version>
	<configuration>
	    <archive>
	        <manifest>
	            <addClasspath>true</addClasspath>  <!-- 在jar包中增加依賴jar路徑說明 -->
	           <mainClass>com.zczy.generator.EntityGenerator</mainClass>
	       </manifest>
	       <!-- 用maven在MANIFEST.MF資料中的Class-Path中增加當前目錄(.)  -->
            <manifestEntries>
               <Class-Path>.</Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>
        
<plugin> 
   <groupId>org.apache.maven.plugins</groupId> 
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.1</version> 
   <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
       <encoding>UTF-8</encoding> 
   </configuration> 
</plugin>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2.1</version>
  <configuration>
  	<appendAssemblyId>false</appendAssemblyId>
  	<archive>
     <manifest>
          <mainClass>com.zczy.generator.EntityGenerator</mainClass>
     </manifest>
	</archive>
   	<descriptors>
    		<descriptor>assembly/assembly.xml</descriptor>
   	</descriptors>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>assembly</goal>
      </goals>
    </execution>
  </executions>
</plugin> 
	 
2. assembly.xml 編輯
<assembly>
  <id>bin</id>
  <formats>
    <format>zip</format>
  </formats>
  <dependencySets>
    <dependencySet>
      <useProjectArtifact>true</useProjectArtifact>
      <outputDirectory>lib</outputDirectory>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>README.txt</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>/scripts</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>gen.bat</include>
      </includes>
    </fileSet>
    <fileSet>
      <directory>/src/main/resources</directory>
      <outputDirectory>/</outputDirectory>		
      <includes>
        <include>gen.properties</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>