1

I need to create jar with all dependencies. I have spring project which are working with database and I need to start spring boot application on another computer, where there is only java. I want to have only jar file and start it with java -jar jarname.jar How to create this jar?

UPD: my pom now:

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

    <properties>
        <!-- The main class to start by executing java -jar -->
        <start-class>app.App</start-class>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.2</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                    <transformer

                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>app.App</mainClass>
                                    </transformer
>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>

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

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

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

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
    </dependencies>

when i try to java -jar jarname.jar in terminal i have exception: Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication

1 Answers1

1

If its a spring boot project (and I assume it is, because the exception states: that the class org/springframework/boot/SpringApplication is missing; this class clearly belongs to the spring-boot project) then you don't need to use maven shade plugin.

Instead use spring-boot-maven-plugin. See here for examples of possible options and configurations

<build>
  ...
  <plugins>
    ...
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>2.2.2.RELEASE</version> // or your version
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
  ...
</build>

The idea behind this plugin is that spring boot is not really a fat jar but something more complicated. It has internal structure that doesn't really look like jar (for example, all the dependencies are packages as JARs in BOOT-INT/lib, so you have jar insider jar)

Alternatively, if its a spring project without spring boot, then clearly you don't need spring boot jars in the classpath at all. In this case you don't need a spring-boot-maven-plugin as well.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97