参考文章:

  Maven生成可以直接运行的jar包的多种方式(http://blog.csdn.net/xiao__gui/article/details/47341385)

 

一般,我们通过 

> mvn package

打出来的jar包,不可运行,缺少清单(META-INF/MANIFEST.MF Main-Class)

> java -jar ****.jar

 

1.插件 maven-assembly-plugin

  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-assembly-plugin</artifactId>
  			<version>2.5.5</version>
  			<configuration>
  				<archive>
  					<manifest>
  						<mainClass>server.jetty.StartJetty</mainClass>
  					</manifest>
  				</archive>
  				<descriptorRefs>
  					<descriptorRef>jar-with-dependencies</descriptorRef>
  				</descriptorRefs>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>

 

命令行:

>mvn package assembly:single 

如果希望在打包时仅仅输入mvn package则:

在configuration后面添加:

  			<executions>
  				<execution>
  					<id>make-assembly</id>
  					<phase>package</phase>
  					<goals>
  						<goal>single</goal>
  					</goals>
  				</execution>
  			</executions>

Spring Framework的项目,这种方式打包会出错,可采用下一种。

2.插件 maven-shade-plugin

<build>  
    <plugins>  
  
        <plugin>  
            <groupId>org.apache.maven.plugins</groupId>  
            <artifactId>maven-shade-plugin</artifactId>  
            <version>2.4.1</version>  
            <executions>  
                <execution>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>shade</goal>  
                    </goals>  
                    <configuration>  
                        <transformers>  
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                                <mainClass>com.xxg.Main</mainClass>  
                            </transformer>  
                        </transformers>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>  
  
    </plugins>  
</build>  

 

>mvn package

这时就可以看到在target下生成了两个jar,运行不带 original的那个jar文件即可。

如果项目中用到了Spring Framework,将依赖打到一个jar包中,运行时会出现读取XML schema文件出错。原因是Spring Framework的多个jar包中包含相同的文件spring.handlers和spring.schemas,如果生成一个jar包会互相覆盖。为了避免互相影响,可以使用AppendingTransformer来对文件内容追加合并

                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                <resource>META-INF/spring.handlers</resource>  
                            </transformer>  
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                                <resource>META-INF/spring.schemas</resource>  
                            </transformer>  

3. 插件SpringBoot-maven-plugin

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.5.3.RELEASE</version>
    <configuration>
        <mainClass>com.xx.**.MyApplication</mainClass>  <!--有多个springboot注解启动时需要指定  -->
<layout>JAR</layout> <!-- JAR ZIP WAR -->
     <skip>true</skip> <!-- 打包时是否跳过测试 --> </configuration> <executions> <execution> <goals> <goal>repackage</goal> <!--会打一份 mvn package 的原有包 *.origin,然后再打一份springboot的jar包--> </goals> </execution> </executions> </plugin>

 

 启动Class选择也可在<properties>中添加:

<properties>
    <start-class>com.xx..MyApplication</start-class> 
</properties>

 

posted on 2017-10-27 14:16  zhaoqiang1980  阅读(216)  评论(0编辑  收藏  举报