Whirvis
Whirvis

Reputation: 312

How to use a custom library in Maven?

I am working on a software that uses Maven. My friend who works with me on it recently added a IO-Lib which makes it easier to handle packets sent by Minecaft: Pocket Edition Click here to see the software I'm talking about But, since the jar is not on the Maven Repo, I can use dependency the normal way. Is there anyway to use a custom library not in the maven repo?

My pom.xml:

<project>
    <?xml version="1.0" encoding="UTF-8"?>

    <groupId>net.trenterprises.diamondcore</groupId>
    <artifactId>DiamondCore</artifactId>
    <version>0.1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.0-rc1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.0-rc1</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>net.trenterprises.diamondcore.run</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <organization>
        <name>Trenterprises</name>
    </organization>
</project>

Upvotes: 2

Views: 8817

Answers (3)

kuujo
kuujo

Reputation: 8185

I think this is what lhasadad was talking about, but to elaborate: I have used <scope>system</scope> and <systemPath>some/path/to.jar</systemPath> to point Maven at local jars successfully in the past. For example:

<dependency>
  <groupId>org.apache</groupId>
  <artifactId>kafka_2.8.2</artifactId>
  <version>${kafka.version}</version>
  <type>jar</type>
  <scope>system</scope>
  <systemPath>${basedir}/lib/kafka_2.8.2-${kafka.version}.jar</systemPath>
</dependency>

Upvotes: 4

Edwin Buck
Edwin Buck

Reputation: 70909

If the custom jar file is not in maven central, the next best thing is for it to be in a different repo. If that's the case, you can add additional repository details to the pom.xml file and it will pull from both repos.

The second best thing is to use the "mvn install:install-file" target to manually install the binary into your cache. Then it will resolve in environments backed by your cache. If you have a build system that lives outside of your working environment, it gets just a little bit harder.

Assuming you have to make this work in multiple maven environments (not just your own) and you don't have a private repository, you need to create one. You can download various maven repository management systems (Artifactory, etc.) install them, and then configure your in-house repository the same way you would add a second out-of-house repository.

If setting up a repository server seems daunting to you, while it is far from a perfect solution, for a little while you can use the "mvn install:install-file" target to install the file in each local cache (but one typo or cache clear and it's going to be messed up in that cache)

Upvotes: 6

LhasaDad
LhasaDad

Reputation: 2143

You may want to add a system scoped dependency. have a look here:

https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

it explains the various ways you can import a dependency. not all require the dependency be held in a repository.

Upvotes: 3

Related Questions