简体   繁体   中英

Two JARs are built for SpringBoot application

I am using spring boot 1.4.1 and have built a very simple application. I see two JARs are getting built (abc.jar and abc-boot.jar) using maven when I use the following pom.xml. How will I avoid building abc.jar as it simply wastes overall build time?

The pom.xml is pasted below -

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>abc</groupId>
    <artifactId>abc</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <!-- Need to avoid putting version in final jar's name -->
        <finalName>abc</finalName>
        <plugins>
            <plugin>
                <!-- Package as an executable jar -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>xyz.Application</mainClass>
                    <layout>ZIP</layout>
                    <classifier>boot</classifier>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

You are getting two jars because you have configured Spring Boot's Maven plugin with a classifier. This means that the fat jar that it produces is created alongside the normal jar.

If you'd like a single jar to be produced and for it to be a fat jar, you should remove the <classifier>boot</classifier> from the plugin configuration.

You are getting two artifacts because the default assembly pluing is creating abc.jar and your added spring-boot-maven-plugin will produce another jar with the name abc-boot.jar.
Alternatively, you can get rid of the spring-boot-maven plugin and still be able to produce one executable jar. This can be done by configuring the maven assembly plugin as shown in the below example.

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          [...]
          <archive>
            <manifest>
              <mainClass>org.sample.App</mainClass>    //specify your spring-boot main class here.
            </manifest>
          </archive>
        </configuration>
        [...]
      </plugin>
      [...]
</project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM