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>
posted @ 2016-11-10 18:06  ding9587  阅读(7460)  评论(0编辑  收藏  举报