0

I've packaged my mvn project.

After that, my jar files only contains my hand coded classes:

META-INF/
META-INF/MANIFEST.MF
me/
me/jeusdi/
me/jeusdi/slab/
me/jeusdi/slab/MyService.class
me/jeusdi/slab/App.class
me/jeusdi/slab/MyConfig.class
me/jeusdi/slab/MyServiceImpl.class
META-INF/maven/
META-INF/maven/me.jeusdi.slab/
META-INF/maven/me.jeusdi.slab/slab/
META-INF/maven/me.jeusdi.slab/slab/pom.xml
META-INF/maven/me.jeusdi.slab/slab/pom.properties

Nevertheless, I need declared maven dependencies:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>me.jeusdi.slab</groupId>
  <artifactId>slab</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>slab</name>
  <url>http://maven.apache.org</url>

  <properties>
    <kafka.streams.version>3.4.0</kafka.streams.version>
    <spring.version>6.0.5</spring.version>

    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>
  
  <dependencies>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-streams</artifactId>
        <version>${kafka.streams.version}</version>
    </dependency>

    <!-- Spring Core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- Spring Context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>

  </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Why spring-core dependency is not present into my jar.

I need to run my jar. Currently, I'm using:

java -cp target/slab-1.0-SNAPSHOT.jar App

Any ideas about how to embed or locate my dependencies and run my application?

Jordi
  • 20,868
  • 39
  • 149
  • 333
  • Did you consider to use spring boot? It provides a maven plugin, that can create executable jars which contain all the needed dependencies: https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html It even can start a web server if needed. And here is the documentation of the spring-boot-maven-plugin: https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins.html#build-tool-plugins.maven – andy Feb 23 '23 at 17:39

2 Answers2

1

You can use Maven to create an uber-jar that contains all the classes. You can as well ask Maven to download the dependencies for your.

What I usually do is to generate the jar the same as you do, but then automatically download all the dependencies into a lib folder. To make running the jar as easy as java -jar ... I have Maven even add the classpath into the manifest. The only drawback is that you now need to distribute the jar and it's libs in exactly this directory structure. The advantage is that any signed jar will preserve it's signature and thus validity.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <outputDirectory>${project.build.directory}/dist</outputDirectory>
                <archive>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib</classpathPrefix>
                        <mainClass>${mainclass}</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/dist/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Queeg
  • 7,748
  • 1
  • 16
  • 42
  • What about [shade plugin](https://maven.apache.org/plugins/maven-shade-plugin/)? – Jordi Feb 24 '23 at 07:01
  • The shade plugin would create the uber-jar I mentioned above. But as I do not use it I don't have an example. Feel free to post it. – Queeg Feb 24 '23 at 07:39
1

If you want to include the jar with dependencies you could add the following

<plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>

Just replace fully.qualified.MainClass in <mainClass>fully.qualified.MainClass</mainClass> with which ever class has main

To Build it you can run mvn clean compile assembly:single

See https://stackoverflow.com/a/574650/5622596 for more details.

As to how to run a jar one possible way is to run the following java -jar NameOfJar.jar Just replace NameOfJar.jar with whichever jar you created

jspek
  • 438
  • 3
  • 10