在pom.xml <build>标签下增加以下配置
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>这里是程序入口 main 方法类</mainClass><!--project 入口 --> </manifest> </archive> </configuration> <executions> <execution> <!--执行器 mvn assembly:assembly --> <id>make-assembly</id> <!--名字任意 --> <phase>package</phase> <!-- 绑定package生命周期阶段上 --> <goals> <goal>single</goal> <!-- 该打包任务只运行一次 --> </goals> </execution> </executions> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.yml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.yml</include> </includes> <filtering>false</filtering> </resource> </resources>
除此之外,我打包还遇到了 定义了相同bean 对象的问题,需要在application.properties 下增加允许覆盖原有对象的配置
spring.main.allow-bean-definition-overriding=true
然后在IntelliJ IDEA 中 点击 右侧maven 完成以下步骤
提示build success 成功,在左侧工程列表中会出现一个target文件夹,里面名称长那个那个jar 就是我们打包生成的可执行文件 ,可以通过java -jar xxx.jar 在控制台运行