6

Basically I have a Maven Project and I am trying to generate a jar of my project using mvn clean compile package through spring tool suite using below pom.xml.

After the command is executed, I get 2 jar files generated in my target folder - {project-name}.jar & {project-name}.jar.original

The .jar file contains Spring boot framework class files having this contents while the .jar.original file contains actual class files of my application.

Snapshot of target folder

I also tried using mvn clean install but no luck with it. Moreover, the only 1 file is installed which is useless for me, because it does not contain any class files of my application but contains spring framework classes.

pom.xml

<?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>com.commonporject</groupId>
    <artifactId>commonporject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>commonporject</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.ibm.db2.jcc</groupId>
            <artifactId>db2jcc4</artifactId>
            <version>10.1</version>
        </dependency>
        <!-- other dependencies -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerVersion>1.8</compilerVersion>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Console:

[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< com.commonporject:commonporject >----------------------
[INFO] Building commonporject 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ commonporject ---
[INFO] Deleting C:\Users\Aadil\Documents\workspace-spring-tool-suite-4-4.0.0.RELEASE\commonporject\target
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ commonporject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ commonporject ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to C:\Users\Aadil\Documents\workspace-spring-tool-suite-4-4.0.0.RELEASE\commonporject\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ commonporject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ commonporject ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ commonporject ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ commonporject ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ commonporject ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.1.0:jar (default-jar) @ commonporject ---
[INFO] Building jar: C:\Users\Aadil\Documents\workspace-spring-tool-suite-4-4.0.0.RELEASE\commonporject\target\commonporject-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.0.RELEASE:repackage (repackage) @ commonporject ---
[INFO] Replacing main artifact C:\Users\Aadil\Documents\workspace-spring-tool-suite-4-4.0.0.RELEASE\commonporject\target\commonporject-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.706 s
[INFO] Finished at: 2018-11-04T12:28:37+05:30
[INFO] ------------------------------------------------------------------------
csfragstaa
  • 145
  • 1
  • 2
  • 9
  • application classes should be in there. did you try to invoke the jar file using `java -jar`? – Sharon Ben Asher Nov 04 '18 at 07:27
  • Why do you use the spring boot plugin, whose purpose is to generate an executable jar for a spring boot application, containing a bootstrapper and all the aplication dependencies, if your goal is precisely to avoid that, and to just generate a regular jar file? – JB Nizet Nov 04 '18 at 07:30
  • You seemed to misunderstand the maven build life cycle if you use `mvn clean compile package` cause this results in multiple executions of things which should be done only once. It is sufficient to use `mvn clean package`....and of course let the spring-boot-maven-plugin do it's work cause it's intended that way and it does it's job very good. – khmarbaise Nov 04 '18 at 09:11
  • @khmarbaise even If I use mvn clean package, and don't remove the spring-boot-maven-plugin, I face the same problem described in the question...It works fine if I remove that plugin. Any ideas? – csfragstaa Nov 04 '18 at 10:32
  • @JBNizet Wasn't aware of that, thanks for sharing...do you have an article on this? – csfragstaa Nov 04 '18 at 10:33
  • https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-build-systems – JB Nizet Nov 04 '18 at 10:38
  • The jar file which is generated `*.jar.original` is the jar file if the dependencies etc. and the spring-boot starter has not been added . The other file which is generated the `*.jar` is packaged by the spring-boot-maven-plugin which contains the starter and all dependencies which are needed. – khmarbaise Nov 04 '18 at 16:14

1 Answers1

2

spring-boot-maven plugin by default uses the repackage goal which packages all dependencies mentioned in the pom file. When this happens, your project's classes and resources jar is renamed to "original".

Please refer the below documentation for more details about this plugin.

https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html

Prasanth Nair
  • 489
  • 3
  • 12