Maven入門系列(二)--設置中央倉庫的方法


原文地址:http://www.codeweblog.com/maven入門系列-二-設置中央倉庫的方法/

Maven倉庫放在我的文檔里好嗎?當然不好,重裝一次電腦,意味着一切jar都要重新下載和發布。

下載的地址是中央倉庫mvnrepository.com,當然,全球很多個倉庫。

資源的坐標簡稱GVA

那么,現在如何修改maven的本地倉庫路徑呢?

Maven入門系列(二)--設置中央倉庫的方法

關鍵在於maven文件夾的config下的settings.xml(E:\IDE\apache-maven-3.3.1\conf\settings.xml)

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

這里的一段注釋可以看到本地倉庫默認在${user.home}/.m2/repository。

(本文出自:http://my.codeweblog.com/u/1156339/blog/396583)

如果在Linux下就是~/.m2/repository

現在如果要改動,就加入相應的標簽。

  <localRepository>E:\Java\maven\repos</localRepository> 

現在我們再編譯下次上一篇博客中的maven_2項目(依賴於maven_1項目的jar,原本已經發布到本體倉庫):

E:\testspace\maven_2>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maventest2 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven -resources-plugin/2.6/maven-resources-plugin-2.6.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven- resources-plugin/2.6/maven-resources-plugin-2.6.pom (8 KB at 3.4 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven -plugins/23/maven-plugins-23.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven- plugins/23/maven-plugins-23.pom (9 KB at 14.3 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/ 22/maven-parent-22.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/2 2/maven-parent-22.pom (30 KB at 23.9 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/apache/11/apache-11 .pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/apache/11/apache-11. pom (15 KB at 14.3 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven -resources-plugin/2.6/maven-resources-plugin-2.6.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven- resources-plugin/2.6/maven-resources-plugin-2.6.jar (29 KB at 16.7 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven -compiler-plugin/3.1/maven-compiler-plugin-3.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven- compiler-plugin/3.1/maven-compiler-plugin-3.1.pom (10 KB at 6.6 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven -plugins/24/maven-plugins-24.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven- plugins/24/maven-plugins-24.pom (11 KB at 5.4 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/ 23/maven-parent-23.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/2 3/maven-parent-23.pom (32 KB at 13.9 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/apache/13/apache-13 .pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/apache/13/apache-13. pom (14 KB at 12.7 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven -compiler-plugin/3.1/maven-compiler-plugin-3.1.jar Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven- compiler-plugin/3.1/maven-compiler-plugin-3.1.jar (42 KB at 14.6 KB/sec) Downloading: https://repo.maven.apache.org/maven2/junit/junit/4.10/junit-4.10.po m Downloaded: https://repo.maven.apache.org/maven2/junit/junit/4.10/junit-4.10.pom (3 KB at 2.1 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.1 /hamcrest-core-1.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.1/ hamcrest-core-1.1.pom (481 B at 0.3 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-parent/1 .1/hamcrest-parent-1.1.pom Downloaded: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-parent/1. 1/hamcrest-parent-1.1.pom (6 KB at 4.9 KB/sec) [WARNING] The POM for com.happyBKs.maven:maventest:jar:0.0.1-SNAPSHOT is missing , no dependency information available [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 21.691 s [INFO] Finished at: 2015-04-06T16:51:35+08:00 [INFO] Final Memory: 10M/108M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal on project maventest2: Could not resolve dependen cies for project com.happyBKs.maven:maventest2:jar:0.0.1-SNAPSHOT: Could not fin d artifact com.happyBKs.maven:maventest:jar:0.0.1-SNAPSHOT -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit ch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please rea d the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyReso lutionException E:\testspace\maven_2> 

結果,又是一堆下載,因為本地倉庫變了,所有的jar要重新下,如junit。但是由於新的本地倉庫中沒有maven_1項目的jar,所以編譯報錯。

這時候,我們將原先我的文檔中大的默認本地倉庫中的所有jar復制到新repos下。

Maven入門系列(二)--設置中央倉庫的方法

這時候在編譯一下

Maven入門系列(二)--設置中央倉庫的方法

已經成功了!

第二個問題:我們如何設置我們線上的中央倉庫。

在maven3.3.1文件夾下的lib下(E:\IDE\apache-maven-3.3.1\lib)有很多jar

其中有一個maven-model-builder-3.3.1.jar,用解壓軟件打開,org\apache\maven\model下有個pom-4.0.0.xml文件

Maven入門系列(二)--設置中央倉庫的方法

打開該文件

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

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- START SNIPPET: superpom --> <project> <modelVersion>4.0.0</modelVersion> <repositories> <repository> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> <releases> <updatePolicy>never</updatePolicy> </releases> </pluginRepository> </pluginRepositories> <build> <directory>${project.basedir}/target</directory> <outputDirectory>${project.build.directory}/classes</outputDirectory> <finalName>${project.artifactId}-${project.version}</finalName> <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory> <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory> <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory> <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> </resource> </resources> <testResources> <testResource> <directory>${project.basedir}/src/test/resources</directory> </testResource> </testResources> <pluginManagement> <!-- NOTE: These plugins will be removed from future versions of the super POM --> <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) --> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> </plugin> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> </plugin> <plugin> <artifactId>maven-release-plugin</artifactId> <version>2.3.2</version> </plugin> </plugins> </pluginManagement> </build> <reporting> <outputDirectory>${project.build.directory}/site</outputDirectory> </reporting> <profiles> <!-- NOTE: The release profile will be removed from future versions of the super POM --> <profile> <id>release-profile</id> <activation> <property> <name>performRelease</name> <value>true</value> </property> </activation> <build> <plugins> <plugin> <inherited>true</inherited> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <inherited>true</inherited> <artifactId>maven-javadoc-plugin</artifactId> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <inherited>true</inherited> <artifactId>maven-deploy-plugin</artifactId> <configuration> <updateReleaseInfo>true</updateReleaseInfo> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project> <!-- END SNIPPET: superpom --> 

可以看到當中的中央倉庫配置:

<repository> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> 

中央倉庫地址是https://repo.maven.apache.org/maven2

如果用瀏覽器登錄該url

Maven入門系列(二)--設置中央倉庫的方法

點擊http://search.maven.org

就會找到中央倉庫的搜索內容的來源

Maven入門系列(二)--設置中央倉庫的方法

log4j、hibernate等都可以在這搜索到。

例如,我搜索hibernate

Maven入門系列(二)--設置中央倉庫的方法

我選擇一個點擊

Maven入門系列(二)--設置中央倉庫的方法

然后選一個版本,就可以看到

Maven入門系列(二)--設置中央倉庫的方法

然后只要利用頁面左側的依賴描述復制下來,就可以使用了。

<dependency> <groupId>hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.4.0.GA</version> </dependency>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM