如何使用Spring Boot构建两个重新包装的jar

时间:2016-09-08 10:09:32

标签: java spring maven spring-boot pom.xml

我正在尝试使用spring-boot-maven-plugin为两个不同的主类构建两个重新包装的jar。我在配置块中添加了两个具有特定mainClass参数的执行块,但似乎插件不尊重它,因为配置块在执行块内而不是在插件级别上,我总是得到

Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.mystuff.tools.b4commandline.Application, com.mystuff.tools.loadtester.Application]

这是pom的maven-spring-boot-maven插件的插件部分。

<plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>${artifactId}</id>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.mystuff.tools.loadtester.Application</mainClass>
                        <finalName>${artifactId}</finalName>
                    </configuration>
                </execution>
                <execution>
                    <id>b4-commandline</id>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <mainClass>com.mystuff.tools.b4commandline.Application</mainClass>
                        <finalName>b4-commandline</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>

对此的任何帮助都会很棒。我在maven-plugin上看到了stackoverflow的配方,但那些不适用于spring-boot。

4 个答案:

答案 0 :(得分:1)

假设它可行,做你要求的就意味着产生的2个罐子除了明显的main-class属性之外都是相同的。 我宁愿建议您打包一个jar并使用弹簧配置文件启动它

$ java -jar -Dspring.profiles.active=profile1 YourApp.jar
$ java -jar -Dspring.profiles.active=profile2 YourApp.jar

这允许您定义2 application-${profile}.properties和@Conditional配置类,所有这些都基于配置文件名称。

答案 1 :(得分:0)

我无法在单个构建中使其工作(我不是说它不可能),但是一个选项是定义 2个maven配置文件

<profiles>
    <profile>
        <id>one</id>
        <activation>
            <property>
                <name>one</name>
            </property>
        </activation>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>default</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                            <configuration>
                                <mainClass>com.xyz.LauncherOne</mainClass>
                                <finalName>one</finalName>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

    </profile>

    <profile>
        <id>two</id>
        <activation>
            <property>
                <name>two</name>
            </property>
        </activation>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>default</id>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                            <configuration>
                                <mainClass>com.xyz.LauncherTwo</mainClass>
                                <finalName>two</finalName>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

然后,使用两个配置文件构建它(使用我的代码示例中的属性激活):

$ mvn -Done clean install
$ mvn -Dtwo clean install

创建:one.jartwo.jar

答案 2 :(得分:0)

您可以使用<classifier>进行此操作。示例:

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.3.RELEASE</version>
                <executions>
                  <execution>
                    <id>Pack application 1</id>
                    <phase>package</phase>
                    <configuration>
                      <finalName>application</finalName>
                      <mainClass>com.test.Application1</mainClass>
                      <outputDirectory>target/application1</outputDirectory>
                      <classifier>1</classifier>
                    </configuration>
                    <goals>
                      <goal>repackage</goal>
                    </goals>
                  </execution>
                  <execution>
                    <id>Pack application 2</id>
                    <phase>package</phase>
                    <configuration>
                      <finalName>application</finalName>
                      <mainClass>com.test.Application2</mainClass>
                      <outputDirectory>target/application2</outputDirectory>
                      <classifier>2</classifier>
                    </configuration>
                    <goals>
                      <goal>repackage</goal>
                    </goals>
                  </execution>
                </executions>
              </plugin>

答案 3 :(得分:0)

似乎您必须首先考虑没有 id execution 和另一个 execution 编号为的> s;然后就可以了。 对我来说工作正常以下:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <!--<id>Batch 1(Don't put this one)</id>-->
                    <phase>package</phase>
                    <configuration>
                        <classifier>1</classifier>
                        <finalName>Core</finalName>
                        <mainClass>com.mainclass1</mainClass>
                    </configuration>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
                <execution>
                    <id>Batch 2</id>
                    <phase>package</phase>
                    <configuration>
                        <classifier>2</classifier>
                        <finalName>BatchA</finalName>
                        <mainClass>com.mainclass2</mainClass>
                    </configuration>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
                <execution>
                    <id>Batch 3</id>
                    <phase>package</phase>
                    <configuration>
                        <classifier>3</classifier>
                        <finalName>BatchB</finalName>
                        <mainClass>com.mainclass3</mainClass>
                    </configuration>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <testFailureIgnore>true</testFailureIgnore>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>