介绍
亲爱的小伙伴们,开源中国的 Maven 库上线咯!为了方便广大开发同学,开源中国特别推出国内的 Maven 中央库,提供高速稳定的网络和服务,为国内 Maven 使用者提供便捷服务。本 Maven 库是从 ibiblio 同步过来的,因为网络等原因,保持每天一次更新。本 Maven 库使用开源软件 Nexus 搭建,对外镜像链接地址为: http://maven.oschina.net/content/groups/public/ 。
使用
接下来将简单介绍如何在您的项目中使用 Maven,以及使用 OSChina 提供的 Maven 服务。
1.安装 Maven如果需要使用到 Maven ,必须首先安装 Maven , Maven 的下载地址在 Apache Maven 中有,您也可以点击这里下载 zip ,tar.gz。
下载好 Maven 后,需要简单安装下。将下载的 zip 或者 tar.gz 包解压到需要安装到的目录。 接下简单配置下环境变量:
1、新建环境变量 M2_HOME ,输入值为 Maven 的安装目录。
2、新建环境变量 M2 ,输入值为: %M2_HOME%\bin 。
3、将 M2 环境变量加入 Path 的最后,如: ;%M2% ;。
环境变量就这么简单配置下就可以了。打开命令行窗口输入 mvn -version 。可以看到如下输出:
看到以上输出,您的 Maven 环境就已经搭建好了。
2.修改 settings.xml在 Maven 中使用 OSChina 的 Maven 服务还需要简单配置一下 Maven,在 Maven 的安装目录下的 conf 文件下有个 settings.xml 文件,接下来我们需要对这个文件做简单的修改,修改前您可以简单备份下该文件。 打开 settings.xml 文件,按下面内容修改。或者点击 settings.xml 下载
点击(此处)折叠或打开
- <mirrors>
- <!-- mirror | Specifies a repository mirror site to use instead of a given
- repository. The repository that | this mirror serves has an ID that matches
- the mirrorOf element of this mirror. IDs are used | for inheritance and direct
- lookup purposes, and must be unique across the set of mirrors. | -->
- <mirror>
- <id>nexus-osc</id>
- <mirrorOf>*</mirrorOf>
- <name>Nexus osc</name>
- <url>http://maven.oschina.net/content/groups/public/</url>
- </mirror>
- </mirrors>
补充: 如果还需要osc的thirdparty仓库或多个仓库,需要如下修改:详情
点击(此处)折叠或打开
- <mirrors>
- <!-- mirror | Specifies a repository mirror site to use instead of a given
- repository. The repository that | this mirror serves has an ID that matches
- the mirrorOf element of this mirror. IDs are used | for inheritance and direct
- lookup purposes, and must be unique across the set of mirrors. | -->
- <mirror>
- <id>nexus-osc</id>
- <mirrorOf>central</mirrorOf>
- <name>Nexus osc</name>
- <url>http://maven.oschina.net/content/groups/public/</url>
- </mirror>
- <mirror>
- <id>nexus-osc-thirdparty</id>
- <mirrorOf>thirdparty</mirrorOf>
- <name>Nexus osc thirdparty</name>
- <url>http://maven.oschina.net/content/repositories/thirdparty/</url>
- </mirror>
- </mirrors>
这里是配置 Maven 的 mirror 地址指向OSChina 的 Maven 镜像地址。 在执行 Maven 命令的时候, Maven 还需要安装一些插件包,这些插件包的下载地址也让其指向 OSChina 的 Maven 地址。修改如下内容。
点击(此处)折叠或打开
- <profile>
- <id>jdk-1.4</id>
-
- <activation>
- <jdk>1.4</jdk>
- </activation>
-
- <repositories>
- <repository>
- <id>nexus</id>
- <name>local private nexus</name>
- <url>http://maven.oschina.net/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>nexus</id>
- <name>local private nexus</name>
- <url>http://maven.oschina.net/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- </releases>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </pluginRepository>
- </pluginRepositories>
- </profile>
如果您需要修改 Maven 的默认文件保存路径,需要在 settings.xml 文件中修改如下地方。
点击(此处)折叠或打开
- <localRepository>F:/Maven/repo/m2/</localRepository>
按照如上修改 settings.xml 之后,您就可以在自己的 Maven 中使用 OSChina 为您提供的 Maven 服务了。
您可以创建一个属于自己的 Maven 项目来简单测试一下 OSChina 的 Maven 库。创建 Maven 项目很简单,只需要简单一行命令就可以搞定。
下面是创建 Maven 项目的命令:
点击(此处)折叠或打开
- mvn archetype:create -DgroupId=oschina -DartifactId=simple -DpackageName=net.oschina.simple -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
其中 -DarchetypeArtifactId=maven-archetype-webapp 代表创建一个简单的 webapp 项目。至于其他参数的意思,您可以查看下 Maven 的相关文档。 在您需要创建 Maven 项目的文件夹下用命令行执行以上命令。可以看到如下输出:
如果顺利创建成功的话,你会看到如下输出:
如果创建失败,就可能需要您去 Google 一下了。创建项目的时候,Maven 会下载一些需要用到的 Maven 插件。
4.添加包依赖 因为只是简单介绍下使用 OSChina 的 Maven 库,关于 Maven 的项目编译,打包,测试,发布等具体细节就不多介绍了,您可以去查看 Maven 的相关文档。
跟ant一样,Maven 也需要通过 xml 来配置。在项目的根目录下有一个 pom.xml 文件。
点击(此处)折叠或打开
- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>oschina</groupId>
- <artifactId>oschina.simple</artifactId>
- <packaging>war</packaging>
- <version>1.0</version>
- <name>oschina.simple Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- <finalName>oschina.simple</finalName>
- </build>
- </project>
在 pom.xml 中加入您需要添加的包,如果您需要查找依赖包有哪些版本,可以在 OSChina Maven 搜索。比如输入 jedis 的结果会如下:
在右下角的 Maven 标签下可以看到当前选中的版本的依赖 xml,copy 该依赖 xml 到 pom.xml 的 dependencies 结点下。如果需要其他依赖包,您也继续搜索。当然,有些偏门的 jar 可能没有,这主要可能是它没有提交到 Maven 的中央库中。
添加好包的依赖之后,您就可以开始在您的 Maven 项目下开始工作了。如果一切准备就绪,您可能需要将项目安装到本地 repository 。
执行命令: mvn clean & mvn install 就可以把项目安装到您配置的本地镜像地址 .m2/repository 下了。 执行命令可以看到如下输出:
执行完之后,如果安装成功会出现如下结果:
如果失败了,就根据失败结果 Google 一下吧! 接下来打开您配置的本地 repository 地址,可以看到您配置的各种依赖包都下载到您本地文件夹了,下次您再使用这些包的时候就不需要再次下载了,项目构建速度也会提高很多。
项目上传说明
开源中国 Maven 库的第三方库中上传的构件信息仅仅存在于本库中,我们不向 Maven 中央库推送任何有关您上传的构件。如果有需要将您的构件上传到 Maven 中央库,请前往 Maven中央库,如果您的构件在中央库中申请通过,并且在本库中存在版本,请通知 @卜祥龙 处理。鉴于上传到本 Maven 库中的构件信息直接放在对外开放的 public group 中会误导用户,此第三方库未配置到 public group ,如果您的项目中有需要用到其中的构件,请在项目中单独配置此地址:http://maven.oschina.net/content/repositories/thirdparty/ 。
感谢
以上就是关于 OSChina Maven 的简单使用说明,多谢您的使用,您的支持就是我们最大的动力,同时也感谢阿里云的服务支持。