2

I am trying to build jar file of my app based on Maven. So I need to not include external jar library's to my build. I need that my app gives this external dependency in runtime from local maven repository or from local folder, those will contain these external libraries. I configure my pom file for this dependency like this:

<profiles>
        <profile>
            <id>compile</id>
            <dependencies>
                <dependency>
                    <groupId>groupId</groupId>
                    <artifactId>some.artifact</artifactId>
                    <version>1.0</version>
                </dependency>
            </dependencies>
        </profile>

and trying to run this jar with this prefix -Pcompile. I am using this answer.

But in the runtime, when I am trying to execute a method, that using this external library, I've got java.lang.NoClassDefFoundError with the name of my class from my external library.

So how I can make a build that will use external jar libraries from local storage?

sForSujit
  • 987
  • 1
  • 10
  • 24
iliya.rudberg
  • 739
  • 12
  • 23
  • Like here: https://stackoverflow.com/questions/11758594/how-do-i-put-all-required-jar-files-in-a-library-folder-inside-the-final-jar-fil – PeterMmm Aug 23 '17 at 08:25
  • It is much more common to build a self-contained executable jar file that includes all dependencies. Makes deployment so much easier and safer. https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven?rq=1 – Thilo Aug 23 '17 at 08:26
  • @Thilo: This does not work if you have a library like Bouncycastle in the dependencies as it is a signed crypto-provider and jar-with dependencies kills the jar-signatures which makes BC to fail. – Robert Aug 23 '17 at 08:48

1 Answers1

1

So i found the solution to store jars outside the final maven build. I was just need to add this jar to classpath with correct path. For this i add this to pom.xml:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>${project.build.finalName}.lib/</classpathPrefix>
                        <mainClass>your.mainClass</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>project-0.0.1-SNAPSHOT.lib/some.lib-1.1.jar</Class-Path>
                        </manifestEntries>

                    </archive>
                </configuration>            
</plugin>
iliya.rudberg
  • 739
  • 12
  • 23