S
S
sawa42018-08-15 19:38:22
Java
sawa4, 2018-08-15 19:38:22

How to build jar from a project that uses maven jar?

All the best!
There is a small Java class that uses 1 Java library (jar file).
Locally, everything is good, everything works, the Maven build is also done, everything is OK, BUT it does not find the library
Run: java -jar Ver-1.0.jar
Gives an error:

Exception in thread "main" java.lang.NoClassDefFoundError:

I scoffed at pom.xml as best I could, the latest version has come to what:
<?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>Ver</groupId>
    <artifactId>Ver</artifactId>
    <version>1.0</version>

    <dependencies>
        <dependency>
            <groupId>libs</groupId>
            <artifactId>lib-1</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>
                ${project.basedir}/src/main/java/lib/lib-1.jar
            </systemPath>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>pc.Start</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5</version>
                <executions>
                    <execution>
                        <id>lib-1</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <groupId>libs</groupId>
                            <artifactId>lib-1</artifactId>
                            <version>1.0</version>
                            <packaging>jar</packaging>
                            <file>${project.basedir}/src/main/java/lib/lib-1.jar</file>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-08-15
@sawa4

The easiest way out of this situation is to build a fat jar that includes the classes of both the program itself and all its dependencies. You can do this by expanding pom.xml a bit:

<project ... >
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Now, when building, it will create two jar files - a regular one with a view name <имя>-<версия>.jarand a thick one with a view name. <имя>-<версия>-jar-with-dependencies.jar
The second way is to ship the application along with dependency jar files. To do this, first, ensure that the Class-Path: section is added to the manifest. You can do this by adding to pom.xml
<project ... >
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Secondly, after the build is completed, you need to run the command mvn dependency:copy-dependencies, by which Maven will copy all the dependencies to target/dependency. After that, you can copy the program jar file and the dependency jar files into the same working directory and run the program.
PS Scripts are not written in Java.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question