How to manually add jar files to the local repository of Maven 3

Source: Internet
Author: User

For Maven projects, most third-party java library files that are commonly used can be automatically downloaded from Maven's Central Repository. However, if the jar files we need are not in the Central Repository, then we need to manually add the downloaded jar file to Maven's local reposotory. In this case, we need to provide Maven with the groupId used to identify jar files (possibly multiple, artifactId, version, and other information.

I am not going to explain how to add a downloaded jar Library to the local repository. We will create a jar library ourselves. For example, we have a simplest HelloWorld class HelloWorld. java:

package com.thoughtworks.davenkin;

public class HelloWorld
{
public void sayHello()
{
System.out.println("Hello, World");
}
}

We want to package HelloWorld. java into a jar file and install it in Maven's local repository for use by other programs.

After compiling and packaging the hello-world.jar, in order to comply with Maven's rules, you need to give the hello-world.jar a version number, so the hello-world.jar is renamed to the hello-world-1.0.jar, then you can use mvn to install this package to Maven's local repository:

mvn install:install-file -Dfile=path/to/hello-world-1.0.jar -DgroupId=com.thoughtworks.davenkin -DartifactId=hello-world -Dversion=1.0 -Dpackaging=jar

Here, the-Dfile option should provide the path to install the jar file. in Linux/Mac, the jar file will be installed in the following directory:

~/.m2/repository/com/thoughtworks/davenkin/hello-world/1.0/hello-world-1.0.jar

Now, our hello-world-1.0.jar can be used by other Maven projects, to create a Maven project:

mvn archetype:generate-DgroupId=com.thoughtworks.davenkin.demo -DartifactId=helloworld-demo -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0

At this time, the helloworld-demo subdirectory will be automatically created under the current directory, switch to the helloworld-demo directory, Delete the existing App. java, and create your own Main. java File

rm src/main/java/com/thoughtworks/davenkin/demo/App.java
touch src/main/java/com/thoughtworks/davenkin/demo/Main.java

Add the following content to the Main. java file:

package com.thoughtworks.davenkin.demo;

import com.thoughtworks.davenkin.HelloWorld;

public class Main
{
public static void main(String[] args)
{
new HelloWorld().sayHello();
}
}

Next is the most important step. Modify the pom. xml file to add the dependency on the HelloWorld class:

<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>com.thoughtworks.davenkin.demo</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>demo</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.thoughtworks.davenkin</groupId>
<artifactId>hello-world</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

The highlighted part above is manually added. Maven will spell out the name of the dependent jar package based on artifactId and version, that is, the artifactId-version.jar. For our example, artifactId is hello-world, version is 1.0, and the resulting jar file is the hello-world-1.0.jar, which is why we need to change the hello-world.jar name to the hello-world-1.0.jar at the beginning.

Next we can compile our Main. java:

mvn compile

The compilation result is stored in the target folder.

Now, we can run the following example:

mvn exec:java -Dexec.mainClass="com.thoughtworks.davenkin.demo.Main"

Output on the author's machine is:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ demo >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ demo <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ demo ---
Hello, World
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.423s
[INFO] Finished at: Wed Feb 15 21:33:47 CST 2012
[INFO] Final Memory: 5M/81M
[INFO] ------------------------------------------------------------------------

The highlighted section above is the expected program output, of course you can also run with traditional java commands, at this time you need to add the hello-world-1.0.jar to classpath, input:

java -cp ~/.m2/repository/com/thoughtworks/davenkin/hello-world/1.0/hello-world-1.0.jar:target/classes/  com.thoughtworks.davenkin.demo.Main

Output:

Hello, World

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.