Maven打成可执行jar包的配置

maven打可执行jar包

本文主要参考:

http://blog.csdn.net/strongyoung88/article/details/54097830

 

首先需要知道如何建立一个maven项目:

mvn archetype:generate -DarchetypeCatalog=internal

 

然后修改pom文件

 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    
    
    <groupId>com.xueyoucto.xueyou</groupId>
    
    <artifactId>MyFirstMavenProject</artifactId>
    
    <version>1.0-SNAPSHOT</version>
    
    <packaging>jar</packaging>
    
    
    <name>MyFirstMavenProject</name>
    
    <url>http://maven.apache.org</url>
    
    
    <properties>
    
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
    </properties>
    
    
    <dependencies>
    
    <dependency>
    
    <groupId>junit</groupId>
    
    <artifactId>junit</artifactId>
    
    <version>3.8.1</version>
    
    <scope>test</scope>
    
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    
    <dependency>
    
    <groupId>org.apache.commons</groupId>
    
    <artifactId>commons-lang3</artifactId>
    
    <version>3.4</version>
    
    </dependency>
    
    </dependencies>
    
    <build>
    
    <plugins>
    
    <plugin>
    
    <groupId>org.apache.maven.plugins</groupId>
    
    <artifactId>maven-compiler-plugin</artifactId>
    
    <configuration>
    
    <source>1.8</source>
    
    <target>1.8</target>
    
    <encoding>utf-8</encoding>
    
    </configuration>
    
    </plugin>
    
    <plugin>
    
    <artifactId>maven-assembly-plugin</artifactId>
    
    <version>3.0.0</version>
    
    <configuration>
    
    <archive>
    
    <manifest>
    
    <mainClass>com.xueyoucto.xueyou.App</mainClass>
    
    </manifest>
    
    </archive>
    
    <descriptorRefs>
    
    <descriptorRef>jar-with-dependencies</descriptorRef>
    
    </descriptorRefs>
    
    </configuration>
    
    <executions>
    
    <execution>
    
    <id>make-assembly</id> <!-- this is used for inheritance merges -->
    
    <phase>package</phase> <!-- bind to the packaging phase -->
    
    <goals>
    
    <goal>single</goal>
    
    </goals>
    
    </execution>
    
    </executions>
    
    </plugin>
    
    </plugins>
    
    </build>
    
    
    
    </project>
    
    


    其中:

 
  1. <plugin>
    
    <groupId>org.apache.maven.plugins</groupId>
    
    <artifactId>maven-compiler-plugin</artifactId>
    
    <configuration>
    
    <source>1.8</source>
    
    <target>1.8</target>
    
    <encoding>utf-8</encoding>
    
    </configuration>
    
    </plugin>

     

  2.  

这段是负责编译的时候使用jdk的版本和编码格式。

<plugin>

<artifactId>maven-assembly-plugin</artifactId>

<version>3.0.0</version>

<configuration>

<archive>

<manifest>

<mainClass>com.xueyoucto.xueyou.App</mainClass>

</manifest>

</archive>

<descriptorRefs>

<descriptorRef>jar-with-dependencies</descriptorRef>

</descriptorRefs>

</configuration>

<executions>

<execution>

<id>make-assembly</id> <!-- this is used for inheritance merges -->

<phase>package</phase> <!-- bind to the packaging phase -->

<goals>

<goal>single</goal>

</goals>

</execution>

</executions>

</plugin>


这段有两个功能,一个是设置Main函数,一个是在jar包中引入依赖的jar包。

 

下面是使用mvn package后生成的jar包。

 

运行这个jar包:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值