maven项目打包(多个main主类)
maven项目打包默认是不会加入依赖包的,这样的话把包拷贝到服务器上会无法执行,需要把依赖包加入到项目jar里面,需要在pom.xml文件中添加如下部分
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.hxl.es.InsertDataEsBulk</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
红色的是运行程序入口
2.然后在idea中进行打包

把生成的包上传到linux服务器执行

[root@localhost myjava]# java -jar myjava_proj-1.0-SNAPSHOT-jar-with-dependencies.jar
这里linux服务器上安装的java版本需要与idea环境安装的一致
[root@localhost myjava]# java -version
java version "21.0.4" 2024-07-16 LTS
Java(TM) SE Runtime Environment (build 21.0.4+8-LTS-274)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.4+8-LTS-274, mixed mode, sharing)
说明:
1.若该项目里还有其他的入口程序,可以使用如下的方式进行运行
java -cp myjava_proj-1.0-SNAPSHOT-jar-with-dependencies.jar org.hxl.es.Test

[root@localhost myjava]# java -cp myjava_proj-1.0-SNAPSHOT-jar-with-dependencies.jar org.hxl.es.Test
the second main Enter!
浙公网安备 33010602011771号