Maven打包可执行jar并且压缩

时间:2022-05-05 18:34:49

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>