Maven references local jar package

Source: Internet
Author: User
Tags sha1

Tutorial:
Http://www.cnblogs.com/dcba1112/archive/2011/05/01/2033805.html

Installation
    1. To download maven:http://maven.apache.org/download.cgi

      $wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.tar.gz
    2. Unzip, copy to a directory, create a soft link:

      $tar -xzvf apache-maven-3.2.5-bin.tar.gz$sudo cp apache-maven-3.2.5 /usr/local/ -rf$cd /usr/bin$sudo ln -s /usr/local/apache-maven-3.2.5/bin/mvn mvn
    3. Adding environment variables

      $sudo vim /etc/profile

      Join:

      export JAVA_HOME=/usr/local/java/export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jarexport PATH=$PATH:$JAVA_HOME/binexport MAVEN_HOME=/usr/local/apache-maven-3.2.5export PATH=${PATH}:${MAVEN_HOME}/bin

Execute the order and let/etc/profile take effect immediately.

$source /etc/profile
    1. To test whether the installation was successful:

      $mvn -vApache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-15T01:29:23+08:00)Maven home: /usr/local/apache-maven-3.2.5Java version: 1.6.0_32, vendor: Sun Microsystems Inc.Java home: /usr/lib/install/jdk-1.6.0_32/jreDefault locale: en_US, platform encoding: UTF-8$java -versionjava version "1.6.0_32"Java(TM) SE Runtime Environment (build 1.6.0_32-b05)OpenJDK (XXX) 64-Bit Server VM (build 20.0-b12-internal, mixed mode)
Using Maven
    1. Building a MAVEN Project

      $mvn archetype:generateChoose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 554:回车Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 1: 1.0-alpha-12: 1.0-alpha-23: 1.0-alpha-34: 1.0-alpha-45: 1.06: 1.1Choose a number: 6:回车(默认为6)Define value for property ‘groupId‘: : test_groupidDefine value for property ‘artifactId‘: : test_artifactidDefine value for property ‘version‘:  1.0-SNAPSHOT: : Define value for property ‘package‘:  test_groupid: : Confirm properties configuration:groupId: test_groupidartifactId: test_artifactidversion: 1.0-SNAPSHOTpackage: test_groupidY: : y

This is a successful build

$cd test_artifactid$lspom.xml  src

Decentralization of Java projects in the src/main/java/directory, unit testing in the src/test/java/directory

  1. Several common commands:

    * mvn -h,不会用时,可寻求帮助* mvn clean compile,将.java类编译为.class文件* mvn clean test, 执行单元测试,会先运行编译* mvn clean package,进行打包,会先运行测试* mvn clean install –Dmaven.test.skip* mvn clean install,将某jar包安装到maven本地仓库中* mvn archetype:generate,快速的搭建项目骨架,输入一些groupId/artifactId/version等信息,由mvn插件自动生成一些必要的依赖和项目骨架
  2. How to add dependencies to a locally infrequently used jar package:

    First we need to understand the two concepts of the MAVEN repository: local warehouses and remote warehouses
    A local repository is a buffer and a subset of remote repositories, and when you build a MAVEN project, you first look for resources from the local repository, and if not, MAVEN downloads from the remote repository to your local repository. This will not need to be downloaded from a remote location the next time you use it.

    For example, if we put some of the jar packages we need to rely on in the project's Lib folder, we need to add the dependencies to the Pom.xml file. However, most jar packages are informal jar packages that are difficult or impossible to download from a remote repository.

    There are several more common practices on the Web:

      • Installed directly in the local repository, however, this practice only works on your local machine, and the other machines still need to execute the installation commands repeatedly.

      • This practice sometimes creates problems by adding system scope tags, such as when a project is being packaged successfully while the project is in progress, but the package dependency error is reported during the run.

      • Build a local remote repository. First, the dependent jar package is passed the following command: GroupID This type is optional, but you need to ensure that it does not exist in the remote repository and is best to be unique, otherwise it will conflict with other dependencies.

        $mvn install:install-file -Dfile=lib/gson-2.2.4.jar -DgroupId=localgroupid -DartifactId=gson -Dversion=2.2.4 -Dpackaging=jar -DlocalRepositoryPath=repo -DcreateChecksum=true

    After executing the command, we look at the tree structure of the Repo folder, where the repo folder actually corresponds to the remote repository:

    $tree -l.`-- localgroupid    `-- gson        |-- 2.2.4        |   |-- gson-2.2.4.jar        |   |-- gson-2.2.4.jar.md5        |   |-- gson-2.2.4.jar.sha1        |   |-- gson-2.2.4.pom        |   |-- gson-2.2.4.pom.md5        |   `-- gson-2.2.4.pom.sha1        |-- maven-metadata-local.xml        |-- maven-metadata-local.xml.md5        `-- maven-metadata-local.xml.sha1

    At this point, we need to add the following dependencies in Pom.xml:

    <dependency>  <groupId>localgroupid</groupId>  <artifactId>gson</artifactId>  <version>2.2.4</version></dependency>

    In addition, you need to add the following in Pom.xml:

    <repositories>  <repository>    <id>project</id>    <url>file://${project.basedir}/repo</url>  </repository></repositories>

    We can compile, test, and package the project when we inject dependencies such as the jar packages we need to rely on to Pom.xml.

    the process of downloading dependencies is as follows: depending on the dependencies in Pom.xml, first go to the remote repository to see if there is a dependency package, if there is a direct download. Otherwise, the dependency is checked from repo, and if there is a direct add to the local warehouse, an error is added. So when we add repo to the project, we can install it to the local repository on other machines without having to repeat the manual or Run command.

What do I do when I create a new project?
    • Build maven fixed-format MAVEN projects using the MVN build command
    • When you rely on a local jar package, the local jar package needs to be installed to the Repo folder when it is added, by setting the GroupID, Artifactid, version, and package to differentiate between the different jar packages and the version number.
    • Add Dependencies to Pom.xml
    • Source Code, Unit test code is placed in the respective folder according to the specification

Maven references local jar package

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.