新建一个springboot项目。
1、项目结构
2、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>build-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>build-project</name>
<description>Maven项目打包生成启动文件</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- 打包文件名称-->
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
<!-- 该插件用于打包时自动生成各个环境的启动文件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="生成 windows 启动文件 begin"/>
<copy tofile="${basedir}/target/start-${project.artifactId}-dev.bat">
<fileset file="${basedir}/target/classes/run/start.bat"/>
<filterset>
<filter token="active" value="dev"/>
<filter token="artifactId" value="${project.artifactId}"/>
<filter token="jar" value="./${project.artifactId}.jar"/>
<filter token="jarName" value="${project.artifactId}.jar"/>
<filter token="args" value="-Xmx255m -Xms255m -Xmn125m -Xss256k"/>
</filterset>
</copy>
<copy tofile="${basedir}/target/start-${project.artifactId}-test.bat">
<fileset file="${basedir}/target/classes/run/start.bat"/>
<filterset>
<filter token="active" value="test"/>
<filter token="artifactId" value="${project.artifactId}"/>
<filter token="jar" value="./${project.artifactId}.jar"/>
<filter token="jarName" value="${project.artifactId}.jar"/>
<filter token="args" value="-Xmx255m -Xms255m -Xmn125m -Xss256k"/>
</filterset>
</copy>
<copy tofile="${basedir}/target/start-${project.artifactId}-prod.bat">
<fileset file="${basedir}/target/classes/run/start.bat"/>
<filterset>
<filter token="active" value="prod"/>
<filter token="artifactId" value="${project.artifactId}"/>
<filter token="jar" value="./${project.artifactId}.jar"/>
<filter token="jarName" value="${project.artifactId}.jar"/>
<filter token="args" value="-Xmx255m -Xms255m -Xmn125m -Xss256k"/>
</filterset>
</copy>
<echo message="生成 windows 启动文件 end"/>
<echo message="生成 centos 启动文件 begin"/>
<copy tofile="${basedir}/target/start-${project.artifactId}-dev.sh">
<fileset file="${basedir}/target/classes/run/start.sh"/>
<filterset>
<filter token="active" value="dev"/>
<filter token="artifactId" value="${project.artifactId}"/>
<filter token="jar" value="./${project.artifactId}.jar"/>
<filter token="args" value="-server -Xmx350m -Xms350m -Xmn256m-Xss128k >/dev/null 2>&1 &"/>
</filterset>
</copy>
<copy tofile="${basedir}/target/start-${project.artifactId}-test.sh">
<fileset file="${basedir}/target/classes/run/start.sh"/>
<filterset>
<filter token="active" value="test"/>
<filter token="artifactId" value="${project.artifactId}"/>
<filter token="jar" value="./${project.artifactId}.jar"/>
<filter token="args" value="-server -Xmx350m -Xms350m -Xmn256m-Xss128k >/test/null 2>&1 &"/>
</filterset>
</copy>
<copy tofile="${basedir}/target/start-${project.artifactId}-prod.sh">
<fileset file="${basedir}/target/classes/run/start.sh"/>
<filterset>
<filter token="active" value="prod"/>
<filter token="artifactId" value="${project.artifactId}"/>
<filter token="jar" value="./${project.artifactId}.jar"/>
<filter token="args" value="-server -Xmx350m -Xms350m -Xmn256m-Xss128k >/prod/null 2>&1 &"/>
</filterset>
</copy>
<copy tofile="${basedir}/target/stop-${project.artifactId}.sh">
<fileset file="${basedir}/target/classes/run/stop.sh"/>
</copy>
<echo message="生成 centos 启动文件 end"/>
<delete dir="${basedir}/target/classes/run" verbose="true"/>
<chmod file="${basedir}/target/*.sh" perm="+x"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、拷贝run包模板到resources下
地址:https://download.csdn.net/download/qq_36942720/88180434
4、生产中需要的配置文件:test / prod / dev
application.yml
spring:
profiles:
active: test
application-test.yml
server:
port: 8700
spring:
application:
name: build-project
application-prod.yml
server:
port: 8700
spring:
application:
name: build-project
application-dev.yml
server:
port: 8700
spring:
application:
name: build-project
5、执行打包命令,生成启动文件
target目录下生成Liunx和Windows启动文件
【注】:
问题:liunx下启动出现 /bin/bash^M: 坏的解释器: 没有那个文件或目录 错误。
原因:dos / windows 和 unix / linux 换行格式编码问题导致,dos / windows 『\n\r 』表示换行 unix / linux 『\n』 表示换行。
解决办法:重新编译启动文件
sed -i 's/\r$//' start-build-project-test.sh
启动文件:文章来源:https://www.toymoban.com/news/detail-635479.html
文章来源地址https://www.toymoban.com/news/detail-635479.html
到了这里,关于Maven 打包生成Windows和Liunx启动文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!