我仍在研究具有两个主要类的单个Maven项目。我现在正在使用配置文件和Maven程序集插件。我尝试了以下方法:
将两个主要类添加到一个清单
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>CombineMains</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>combineMains</finalName>
<archive>
<manifest>
<mainClass>com.fruit.Kiwi</mainClass>
<mainClass>com.vegetable.Carrot</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
将两个清单添加到一个存档中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>CombineMains</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>combineMains</finalName>
<archive>
<manifest>
<mainClass>com.fruit.Kiwi</mainClass>
</manifest>
<manifest>
<mainClass>com.vegetable.Carrot</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
将两个档案添加到一个执行中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>CombineMains</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>combineMains</finalName>
<archive>
<manifest>
<mainClass>com.fruit.Kiwi</mainClass>
</manifest>
</archive>
<archive>
<manifest>
<mainClass>com.vegetable.Carrot</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
使用两种不同的执行方式
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>Main1</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>kiwi</finalName>
<archive>
<manifest>
<mainClass>com.fruit.Kiwi</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
<execution>
<id>Main2</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>carrot</finalName>
<archive>
<manifest>
<mainClass>com.vegetable.Carrot</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</executions>
</plugin>
不幸的是,这些解决方案不起作用。每个解决方案都会发生相同的事情。第二个mainClass覆盖第一个。我这样构建罐子:
mvn clean package -DskipTests -P Kiwi -P Carrot
然后我像这样运行jar,以便在配置文件之间切换:
java -jar -Dspring.profiles.active=Kiwi target/hugeJar.jar
java -jar -Dspring.profiles.active=Carrot target/hugeJar.jar
我注意到的是,包含最后构建的主文件的配置文件是唯一可以正确运行的配置文件,而另一个配置文件却没有运行。如果胡萝卜是最后建造的,胡萝卜就会运行,但猕猴桃不会。如果猕猴桃是最后建成的,则猕猴桃会运行,但胡萝卜不会。对我应该做什么建议?
答案 0 :(得分:1)
根据doc,可执行jar具有一个主类,该主类在清单中声明为:
Main-Class: classname
不支持甚至没有多个主类的概念。
您的选择是:
java -cp myjar.jar my.other.MainClass
,因为两者的命令已经不同了