简体   繁体   中英

Installing 3rd Party Jar with Maven

I'm trying to install and use 3rd Party Jars in my Maven project.
The first Jar is:

anthill3-client.jar
And this is his structure:

├───com
│   └───urbancode
│       ├───anthill3
│       │   ├───command
│       │   ├───custom
│       │   ├───dashboard
│       │   ├───domain
│       │   └───wsviewer
│       ├───codestation2
│       │   ├───domain
│       │   └───server
│       ├───commons
│       │   ├───db
│       │   ├───logfile
│       │   └───util
│       ├───license
│       ├───persistence
│       │   └───collections
│       └───scripting
└───META-INF

I installed it by running this command:

call mvn install:install-file -Dfile=anthill3-client.jar -DgroupId=com.urbancode -DartifactId=anthill3-client -Dversion=1.0 -Dpackaging=jar

Configured it in the pom.xml:

<dependency>
  <groupId>com.urbancode</groupId>
  <artifactId>anthill3-client</artifactId>
  <version>1.0</version>
</dependency>

And was able to use it in my code by importing some classes:

import com.urbancode.anthill3.domain.persistent.PersistenceException;
import com.urbancode.anthill3.domain.security.AuthorizationException;
import com.urbancode.anthill3.main.client.AnthillClient;
import com.urbancode.anthill3.persistence.UnitOfWork;
import com.urbancode.anthill3.domain.project.*;

It didn't quite work though, cause I get .ClassNotFoundException :

Exception in thread "main" java.lang.NoClassDefFoundError: com/urbancode/devilfish/services/method/MethodCall
    at Valor.CM.Tools.App.main(App.java:26)
Caused by: java.lang.ClassNotFoundException: com.urbancode.devilfish.services.method.MethodCall
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

I tried to fix it by installing the missing Jar:

devilfish.jar
His structure:

├───com
│   └───urbancode
│       ├───command
│       │   ├───shell
│       │   └───var
│       ├───commons
│       │   └───util
│       ├───devilfish
│       │   ├───client
│       │   ├───common
│       │   ├───server
│       │   └───services
│       └───plugin
└───META-INF

Installed it by running this command:

call mvn install:install-file -Dfile=devilfish.jar -DgroupId=com.urbancode -DartifactId=devilfish -Dversion=1.0 -Dpackaging=jar

And added to my pom:

<dependency>
  <groupId>com.urbancode</groupId>
  <artifactId>devilfish</artifactId>
  <version>1.0</version>
</dependency>

But importing classes from the Package simply doesn't work, and I keep getting the same exception. I guess it happens cause I didn't properly install the Jars. I filled the -DgroupId & -DartifactId based on what made sense to me, but I'm not sure I filled the right values.

Not sure how you are running your project, but it's ok on compilation time, and fails of run time so I guess you are not building you jar right.

In order you pack your jar with dependencies, follow this: How can I create an executable JAR with dependencies using Maven?

<build>
 <plugins>
  <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>
 </plugins>
</build>

Open the devilfish.jar (with a zipping tool) and move to META-INF/maven/[several-packages]/pom.xml. Open its pom.xml and look up the right group, artifact name and version. Then install it again with appropriate settings. If urbancode is your company, install an Artifactory or Nexus server for you company, post the files there and add this server to your pom.xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM