maven项目打包部署到服务器上

tech2023-07-27  105

创建一个maven项目,在本地工程依赖jar包启动完成后,是不够的,还需将项目文件使用maven编译打包然后部署到服务器上

主流的springboot项目打包的时候需要在pom文件中指定主方法等配置

解决打包部署的时候报错no main manifest attribute, in  ..jar

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <!--解决打包部署报错no main manifest attribute, in 。。。jar--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.csrcb.AppStart</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <!--热部署配置--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--fork:如果没有该项配置,整个devtools不会起效果--> <fork>true</fork> </configuration> </plugin> </plugins> </build>

然后使用cmd指令或者在idea

使用指令

mvn clean

然后执行mvn package指令

mvn package

打包好的jar包在target目录下,然后打开文件路径

找到打包好的jar包放到服务器上,然后执行指令,最好在执行命令中增加编码格式UTF-8,即-Dfile.encoding=UTF-8

java -jar springboot_demo-1.0-SNAPSHOT.jar //最好使用当然配置其余的虚拟机参数也在启动脚本中配置 //java -jar -Dfile.encoding=UTF-8 springboot_demo-1.0-SNAPSHOT.jar

前提服务器安装有jdk

 

普通的java项目若不包含spring项目,可以在pom.xml文件中使用下面的配置进行打包,对应的启动类改下即可

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.4</version> <configuration> <forkMode>once</forkMode> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> <!--将依赖jar包打入最终需要打的包中,使用maven指令mvn package--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.csrcb.AppStart</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> <!--使用此方法打包是不是使用mvn package指令,而是mvn assembly:assembly指令--> <!--<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <archive> <manifest> <mainClass>com.csrcb.AppStart</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin>--> </plugins> </build>

若打包过程中出现了编码GBK的不可映射字符报错

依旧在pom文件中添加配置指定编码集utf-8

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

 

 

最新回复(0)