Maven - 实例-2-使用本地仓库中的依赖包

Maven引入构建包的流程

执行mvn compile命令编译源代码,如果编译过程中需要用到其他的包,

maven将会在pom.xml文件中查找是否引入该依赖包的坐标。

示例:

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>

引入坐标之后,将会在本地仓库中查找

如果本地仓库中的没有相应的依赖包,默认继续在中央或远程仓库查找,下载后会放到本地仓库。

示例:使用本地仓库中的依赖包

1 - 安装依赖包到本地仓库中

mvn install 安装jar包到本地仓库中:anliven.testmaven01.HelloMaven

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom
Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom (0 B at 0 B/s)
Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.jar
Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.jar (0 B at 0 B/s)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running anliven.testmaven01.HelloMavenTest
Run test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ testmaven ---
[INFO] Building jar: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\testmaven-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ testmaven ---
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom (2.5 kB at 4.7 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus/3.1/plexus-3.1.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus/3.1/plexus-3.1.pom (19 kB at 24 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom (1.1 kB at 1.9 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom (5.0 kB at 8.9 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom (7.2 kB at 13 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom (7.3 kB at 13 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar (12 kB at 9.8 kB/s)
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar (230 kB at 179 kB/s)
[INFO] Installing D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\testmaven-0.0.1-SNAPSHOT.jar to D:\DownLoadFiles\apache-maven-repo\anliven\testmaven01\testmaven\0.0.1-SNAPSHOT\testmaven-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\Anliven-Running\Zen\EclipseProjects\TestMaven\pom.xml to D:\DownLoadFiles\apache-maven-repo\anliven\testmaven01\testmaven\0.0.1-SNAPSHOT\testmaven-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.602 s
[INFO] Finished at: 2017-10-20T17:14:19+08:00
[INFO] Final Memory: 18M/210M
[INFO] ------------------------------------------------------------------------

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l
total 5
-rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
drwxr-xr-x 1 guowli 1049089   0 Oct 19 13:21 src/
drwxr-xr-x 1 guowli 1049089   0 Oct 20 17:14 target/

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 4
drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:14 classes/
drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:14 maven-archiver/
drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:14 maven-status/
drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:14 surefire-reports/
drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:14 test-classes/
-rw-r--r-- 1 guowli 1049089 2150 Oct 20 17:14 testmaven-0.0.1-SNAPSHOT.jar

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$

2 - 创建使用本地仓库依赖包的Maven项目(包含代码)

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects
$ ls -l
total 4
drwxr-xr-x 1 guowli 1049089 0 Sep  1 16:50 Test/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 17:14 TestMaven/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 17:22 TestMaven02/

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects
$ cd TestMaven02/

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$ ls -l
total 1
-rw-r--r-- 1 guowli 1049089 729 Oct 20 17:13 pom.xml
drwxr-xr-x 1 guowli 1049089   0 Oct 20 16:56 src/

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$ cat pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>anliven.testmaven02</groupId>
        <artifactId>testmaven02</artifactId>
        <version>0.0.1-SNAPSHOT</version>

        <dependencies>
                <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.10</version>
                </dependency>
                <dependency>
                        <groupId>anliven.testmaven01</groupId>
                        <artifactId>testmaven</artifactId>
                        <version>0.0.1-SNAPSHOT</version>
                </dependency>
         </dependencies>

</project>

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$ cat src/main/java/anliven/testmaven02/Speak.java
package anliven.testmaven02;

import anliven.testmaven01.HelloMaven;

public class Speak {

        public String sayHi() {
                return new HelloMaven().sayHello();
        }

}

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$ cat src/test/java/anliven/testmanven02/SpeakTest.java
package anliven.testmaven02;

import org.junit.*;
import org.junit.Assert.*;

public class SpeakTest {
        @Test
        public void testsayHi() {
                System.out.println("Run test!");
                Assert.assertEquals("Hello Maven!", new Speak().sayHi());
        }

}

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$

3- 运行mvn complie、test、package命令

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$ ll
total 1
-rw-r--r-- 1 guowli 1049089 729 Oct 20 17:13 pom.xml
drwxr-xr-x 1 guowli 1049089   0 Oct 20 16:56 src/

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven02 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven02 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven02 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.067 s
[INFO] Finished at: 2017-10-20T17:28:30+08:00
[INFO] Final Memory: 15M/304M
[INFO] ------------------------------------------------------------------------

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven02 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven02 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven02 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven02 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven02 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven02 ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running anliven.testmaven02.SpeakTest
Run test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.468 s
[INFO] Finished at: 2017-10-20T17:28:50+08:00
[INFO] Final Memory: 15M/271M
[INFO] ------------------------------------------------------------------------

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven02 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven02 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven02 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven02 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven02 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven02 ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running anliven.testmaven02.SpeakTest
Run test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ testmaven02 ---
[INFO] Building jar: D:\Anliven-Running\Zen\EclipseProjects\TestMaven02\target\testmaven02-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.830 s
[INFO] Finished at: 2017-10-20T17:29:05+08:00
[INFO] Final Memory: 17M/258M
[INFO] ------------------------------------------------------------------------

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$ ls -l target/
total 4
drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:28 classes/
drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:29 maven-archiver/
drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:28 maven-status/
drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:28 surefire-reports/
drwxr-xr-x 1 guowli 1049089    0 Oct 20 17:28 test-classes/
-rw-r--r-- 1 guowli 1049089 2202 Oct 20 17:29 testmaven02-0.0.1-SNAPSHOT.jar

[email protected] MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$
时间: 2024-10-10 03:01:29

Maven - 实例-2-使用本地仓库中的依赖包的相关文章

maven本地仓库中存在jar包,但编译不成功,显示jar包不存在

介绍一下背景,项目要迁移进坑人的离线的内网开发,将在同事那编译通过的代码和maven仓库拷进内网,打算编译通过之后再上传私服,结果配好maven之后,本地库中的部分jar包显示没有引入,如下面的波浪线 想到可能是idea提示的不正确,直接编译吧,结果编译失败,主要错误信息如下 [ERROR] Failed to execute goal on project zeus-common-redis: Could not resolve dependencies for project com.hsj

在eclipse的maven插件中搜寻本地仓库中的jar搜索不到的解决方案

在eclipse的maven插件中搜寻本地仓库中的jar搜索不到的解决方案 在eclipse的maven插件中搜索本地仓库中的jar搜索不到的解决方案 之前,用过maven管理项目的童鞋都知道本地会有一个${User_Home}.m2/repository仓库 是用来存放jar包的地方.但是,在eclipse中的maven仓库中 搜索不到本地仓库中的jar包. 原因是因为maven中的本地仓库的index索引没有更新,导致在eclipse的maven插件中搜索不到. 解决方案: 在eclipse

maven自定义jar到本地仓库

Apache Maven为项目构建提供了绝佳的解决方案,其本地仓库中缓存了远程代理仓库或中央仓库中的资源,从而提高网络资源使用效率,很好很强大!  但是并非所有资源都可以根据GroupId.ArtifactId和Version信息从中央或代理仓库中获取.例如Oracle数据库的驱动程序,由于版权的原因,任何组织提供的公网Maven仓库都不可能提供该JAR包资源.而这时如果我们的项目使用Maven进行构建,就可以选择将Oracle的驱动程序JAR包作为Maven构件安装到本地仓库中供开发使用. 

Maven本地仓库中jar包更新的依据

对于某个jar包,Maven如何判断远程仓库的jar包比本地仓库中的相应jar包要新呢?当我重新部署到私服上以后,下次执行maven命令的时候会自动下载最新的jar包,那么maven怎么知道要更新呢? 对于这个问题,在网上也没找到十分确切的答案. 所以,我大胆猜测,可能是通过时间戳或者校验和之类的来判断jar包的新旧程度. 猜想1:时间戳 当部署的时候,记录一个当前时间戳,之后我们下载到本地仓库的jar的时间戳就是这个jar包最后一次更新的时间戳.此后,重新打包部署这个jar包,时间戳也随之改变

zbb20181007 maven 将任意jar安装到本地仓库中

maven 将任意jar安装到本地仓库中 假如你要把gavin-2.0.1.jar打包到本地仓库 1.执行以下命令: mvn install:install-file -Dfile=gavin-2.0.1.jar -DgroupId=com.gavin -DartifactId=gavin -Dversion=2.0.1 -Dpackaging=jar 2.将maven的坐标引入项目即可: <dependency>   <groupId>com.gavin</groupId&

如何将本地的jar包村猝倒maven本地仓库中

首先需要将本地的jar包做mvn install到本地仓库中 mvn install:install-file -Dfile=D:\skyeye-ruleInfo\lib\lucene-queryparser-4.4.0.jar -DgroupId=org.apache.lucene -DartifactId=lucene-queryparser -Dversion=4.4.0 -Dpackaging=jar 这个过程 会借助 setting.xml中配置的本地路径 将这个jar包放到本地仓库中

本地jar包 安装到本地仓库中的命令

maven 项目 本地jar包 安装到本地仓库中去: 首先进入到该文件所在文件夹内 若不在直接绝对路径就可以.注意命令中的空格 mvn install:install-file  -Dfile=文件名称(如:md5.jar) -DgroupId=com.capinfo.crypt -DartifactId=md5 -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true

GitHub傻瓜式教程(二、对本地仓库中修改后的文件做同步上传)

前一片博客讲了本地仓库的创建,现在是不是想写写代码分享呢? 博客中涉及的git命令 GUI的操作 最初美好 添加自己的新代码,同步到本地仓库(123.txt) 将本地仓库中的代码提交到github仓库中 欣赏结果 代码行操作 初始状态 添加自己的代码文件(456.txt) 命令行操作 最后的结果

maven项目或者SpringBoot项目启动时报错在本地仓库中找不到jar包的解决办法

经常遇到项目检出来后是导入开发工具eclipse中pom文件出错问题,项目启动时遇到了一些列的jar包找不到的问题,所以换个开发平台到IDEA以为会好些,结果同样的问题还是会出现的,为了找到具体的解决办法,针对以下我遇到的此类问题做总结,希望能帮助到一部分人,所以帮我此次的解决办法记录下来,帮助大家也方面自己. 一.问题现象 二.解决办法 (1)从报错截图中大概知道是因为在本地的maven仓库中找不到“mchange-commons-java-0.2.9.jar ”这个jar包 所以就到本地的m