Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
maven有两个主要的java类。通过终端分别运行每一个_Java_Maven - Fatal编程技术网

maven有两个主要的java类。通过终端分别运行每一个

maven有两个主要的java类。通过终端分别运行每一个,java,maven,Java,Maven,我有一个包含两个主要类的maven项目,并希望通过如下命令分别运行每个类: java -jar appName.jar -cp com.green.RunApp1 java -jar appName.jar -cp com.blue.RunApp2 我尝试在execute标记中将绝对路径添加到这两个类中,但这样做不允许我单独运行它们。 例如,无论运行jar时指向哪个主类,RunApp1都将始终运行。我很确定我忽略了一些次要的东西 My pom.xml: <build> &l

我有一个包含两个主要类的maven项目,并希望通过如下命令分别运行每个类:

java -jar appName.jar -cp com.green.RunApp1
java -jar appName.jar -cp com.blue.RunApp2
我尝试在execute标记中将绝对路径添加到这两个类中,但这样做不允许我单独运行它们。
例如,无论运行jar时指向哪个主类,RunApp1都将始终运行。我很确定我忽略了一些次要的东西

My pom.xml:

 <build>
   <resources>
      <resource>
         <directory>src/main/resources</directory>
         <filtering>false</filtering>
      </resource>
   </resources>
   <testResources>
      <testResource>
         <directory>src/test/resources</directory>
         <filtering>false</filtering>
      </testResource>
   </testResources>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-resources-plugin</artifactId>
         <version>2.7</version>
         <configuration>
            <encoding>UTF-8</encoding>
         </configuration>
         <executions>
            <execution>
               <id>default-resources</id>
               <phase>process-resources</phase>
               <goals>
                  <goal>resources</goal>
               </goals>
            </execution>
            <execution>
               <id>default-testResources</id>
               <phase>process-test-resources</phase>
               <goals>
                  <goal>testResources</goal>
               </goals>
            </execution>
         </executions>
      </plugin>
      <plugin>
         <artifactId>maven-compiler-plugin</artifactId>
         <version>3.5.1</version>
         <configuration>
            <source>1.8</source>
            <target>1.8</target>
         </configuration>
         <executions>
            <execution>
               <id>default-testCompile</id>
               <phase>test-compile</phase>
               <goals>
                  <goal>testCompile</goal>
               </goals>
            </execution>
            <execution>
               <id>default-compile</id>
               <phase>compile</phase>
               <goals>
                  <goal>compile</goal>
               </goals>
            </execution>
         </executions>
      </plugin>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration />
      </plugin>
      <plugin>
         <artifactId>maven-deploy-plugin</artifactId>
         <version>2.8.1</version>
         <configuration>
            <skip>true</skip>
         </configuration>
         <executions>
            <execution>
               <id>default-deploy</id>
               <phase>deploy</phase>
               <goals>
                  <goal>deploy</goal>
               </goals>
            </execution>
         </executions>
      </plugin>
      <plugin>
         <groupId>org.sonatype.plugins</groupId>
         <artifactId>nexus-staging-maven-plugin</artifactId>
         <version>1.3</version>
         <executions>
            <execution>
               <id>default-deploy</id>
               <phase>deploy</phase>
               <goals>
                  <goal>deploy</goal>
               </goals>
            </execution>
         </executions>
         <configuration>
            <skipStaging>true</skipStaging>
         </configuration>
      </plugin>
   </plugins>
</build>

src/main/resources
假的
src/测试/资源
假的
org.apache.maven.plugins
maven资源插件
2.7
UTF-8
默认资源
过程资源
资源
默认测试资源
过程测试资源
测试资源
maven编译器插件
3.5.1
1.8
1.8
默认测试编译
测试编译
测试编译
默认编译
编译
编译
org.springframework.boot
springbootmaven插件
maven部署插件
2.8.1
真的
默认部署
部署
部署
org.sonatype.plugins
NexusMaven插件
1.3
默认部署
部署
部署
真的


您需要在部分中使用不同的ID指定主类。大概是这样的:

<execution>
    <id>main1</id>
    <configuration>
        <mainClass>Main1</mainClass>
    </configuration>
</execution>
<execution>
    <id>main2</id>
    <configuration>
        <mainClass>Main2</mainClass>
    </configuration>
</execution>

main1
Main1
缅因州2
缅因州2

通过制作两个不同的配置文件,我可以让它工作,但我必须为它工作构建两个不同的jar,这与目的背道而驰。这是我的新pom.xml

<profiles>
    <profile>
        <id>Tiger</id>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>false</filtering>
                </resource>
            </resources>
            <testResources>
                <testResource>
                    <directory>src/test/resources</directory>
                    <filtering>false</filtering>
                </testResource>
            </testResources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                    <executions>
                        <execution>
                            <id>default-resources</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>resources</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-testResources</id>
                            <phase>process-test-resources</phase>
                            <goals>
                                <goal>testResources</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                    <executions>
                        <execution>
                            <id>default-testCompile</id>
                            <phase>test-compile</phase>
                            <goals>
                                <goal>testCompile</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-compile</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.abc.Tiger</mainClass>
                        <jvmArguments>
                                -Dtest.dbUser=${test.dbUser}
                                -Dtest.dbPassword=${test.dbPassword}
                                -Dtest.environment=${test.environment}
                            </jvmArguments>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.1</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                    <executions>
                        <execution>
                            <id>default-deploy</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>deploy</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.sonatype.plugins</groupId>
                    <artifactId>nexus-staging-maven-plugin</artifactId>
                    <version>1.3</version>
                    <executions>
                        <execution>
                            <id>default-deploy</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>deploy</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <serverId>nexus</serverId>
                        <skipStaging>true</skipStaging>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>Lion</id>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>false</filtering>
                </resource>
            </resources>
            <testResources>
                <testResource>
                    <directory>src/test/resources</directory>
                    <filtering>false</filtering>
                </testResource>
            </testResources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                    <executions>
                        <execution>
                            <id>default-resources</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>resources</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-testResources</id>
                            <phase>process-test-resources</phase>
                            <goals>
                                <goal>testResources</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                    <executions>
                        <execution>
                            <id>default-testCompile</id>
                            <phase>test-compile</phase>
                            <goals>
                                <goal>testCompile</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-compile</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.abc.Lion</mainClass>
                        <jvmArguments>
                                -Dtest.dbUser=${test.dbUser}
                                -Dtest.dbPassword=${test.dbPassword}
                                -Dtest.environment=${test.environment}
                            </jvmArguments>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.1</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                    <executions>
                        <execution>
                            <id>default-deploy</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>deploy</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.sonatype.plugins</groupId>
                    <artifactId>nexus-staging-maven-plugin</artifactId>
                    <version>1.3</version>
                    <executions>
                        <execution>
                            <id>default-deploy</id>
                            <phase>deploy</phase>
                            <goals>
                                <goal>deploy</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <serverId>nexus</serverId>
                        <skipStaging>true</skipStaging>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

这不是我所期望的。相反,我想构建一个jar,并在单个jar内的配置文件之间切换。有人能告诉我正确的方向吗?

为什么要在构建过程中运行它们?您希望将它们绑定到哪个阶段?
mvn clean package -DskipTests  -P Tiger
java -jar -Dspring.profiles.active=Tiger target/amazingApp.jar
mvn clean package -DskipTests  -P Lion
java -jar -Dspring.profiles.active=Lion target/amazingApp.jar