linux 上 maven 和 nexus3 的详细过程
背景介绍
之前因为做新技术研究,短暂的玩过 nexus3 两天,基于了解也写过一篇 nexus3 搭建的简述,本以为后续要用的时候可以基于简述文档快速上手。结果等到实际工作中,我个人需要完整的设计和搭建一套可用的私服用于公司开发,回过头看文档才发现,之前的了解及文档难以起到有效帮助,故在原文上重新编辑一份。同时也惊醒自己,做技术研究,不说吃透,但一定要有可见可使用的 DEMO。没有达到实际的效果,一切研究都是泡沫。
为何要自己要在 linux 上搭建 maven 和 nexus 好的文章
在 linux 上搭建 maven 个人暂时觉得没有必要,因为 maven 一般在本地开发环境使用。可能一些发布集成平台提供集成 maven 来共享 jar 包。
在 linux 上搭建 nexus 提供隶属于自己的 私服仓库。暂时主要用于管理 个人或组织的私有 jar 包。
前言强调
- jar 包上传到私服 和 从私服下载 jar 包是两套互不干扰的动作。故分开学习和实现 demo 比较好
- jar 包上传和 下载 jar 包都可以在 maven 的 setting.xml 文件中或者工程的 pom.xml 文件中实现。考虑到集中修改共用的优点,我选择在 maven setting.xml 中集中配置。 PS : jar 包上传 pom.xml 必须涉及修改
- setting.xml 文件中 涉及很多 id 的配置。我之前很纠结这些 id 是否一定和 nexus3 上仓库的 id(name) 一一对应。后经过验证。除了 镜像路径的URL 及 账号 密码要对应的上,其他都可以自己有设置。setting.xml 中 name 的设置就更不用说了,随意。
工具介绍
- idea
- nexus3
- maven
配置过程详解
maven 父工程不可缺少
- maven 工程 pom.xml 标签详解
- maven 工程 依赖、继承关系详解
之所以要定义父工程。
其一是因为一般 maven 工程架构设计,都会有
<packaging>pom</packaging>
的父工程。用于统一维护各 jar 包的版本。
其二是可以把上传 jar 的一些统一操作 build 或其他 plug 的配置写在父工程,其子工程可以统一继承。如下:<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>com.taotao</groupId> <artifactId>taotao-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- 集中定义依赖版本号 --> <properties> <junit.version>4.12</junit.version> <spring.version>4.1.3.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mybatis.paginator.version>1.2.15</mybatis.paginator.version> <mysql.version>5.1.32</mysql.version> <slf4j.version>1.6.4</slf4j.version> <jackson.version>2.4.2</jackson.version> <druid.version>1.0.9</druid.version> <httpclient.version>4.3.5</httpclient.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <joda-time.version>2.5</joda-time.version> <commons-lang3.version>3.3.2</commons-lang3.version> <commons-io.version>1.3.2</commons-io.version> <commons-net.version>3.3</commons-net.version> <pagehelper.version>3.4.2</pagehelper.version> <!-- <pagehelper.version>5.1.11</pagehelper.version>--> <jsqlparser.version>0.9.1</jsqlparser.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <jedis.version>2.7.2</jedis.version> <solrj.version>4.10.3</solrj.version> </properties> <dependencyManagement> <dependencies> <!-- 时间操作组件 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda-time.version}</version> </dependency> <!-- Apache工具组件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>${commons-net.version}</version> </dependency> <!-- Jackson Json处理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>${httpclient.version}</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <!-- 日志处理 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>${mybatis.paginator.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!-- JSP相关 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <!-- Redis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency> <!-- solr客户端--> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>${solrj.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- 资源文件拷贝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> </build> <distributionManagement> <repository> <id>releases</id> <name>User Project Release</name> <url>http://47.99.69.84:8081/repository/maven-releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <name>User Project SNAPSHOTS</name> <url>http://47.99.69.84:8081/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement>
nexus 私服搭建
上传 jar 包到私服
标题链接的文章,上传 jar 包到私服教程还是非常好的。
pom.xml 的配置建议写在父工程,这样子工程可以直接继承使用
从 私服上拉取 jar 包。先了解 setting.xml 标签
直接上配置图
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>E:\localRepository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>maven-XX</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>maven-XX</id>
<name>maven-public</name>
<url>http://47.99.69.84:8081/repository/maven-public/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>yiebang-maven-central</id>
<repositories>
<repository>
<id>maven-XX</id>
<name>maven-public</name>
<url>http://nexus-releases</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- 插件仓库列表 -->
<pluginRepositories>
<pluginRepository>
<id>maven-XX</id>
<name>maven-public</name>
<url>http://nexus-releases</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>yiebang-maven-central</activeProfile>
</activeProfiles>
</settings>
- 再次回顾强调点。setting.xml 文件中 涉及很多 id 的配置。我之前很纠结这些 id 是否一定和 nexus3 上仓库的 id(name) 一一对应。后经过验证。除了 镜像路径的URL 及 账号 密码要对应的上,其他都可以自己有设置。setting.xml 中 name 的设置就更不用说了,随意。
- 当前网上比较流行的配置都是 nexus2 。存在一定的误导性,很多 配置的 URL 于 nexus3 不一样,但是并未说明。如:
- setting.xml 文件配置
<servers>
<server>
// 这类 id 是可以随意命名的 但是 工程里面的 pom.xml 文件中配置上传驱动时,要与这里一一对应
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<name>nexus repository</name>
<mirrorOf>*</mirrorOf>
// 此处的地址就是 下列图示地址
<url>http://47.99.69.84:8081/repository/maven-public/</url>
</mirror>
</mirrors>
- pom.xml 文件配置
- 其他的配置都无所谓 ,只有这个最重要。 id 与 setting.xml 中一一对应。URL 取自上方图片
<distributionManagement>
<repository>
<id>releases</id>
<name>User Project Release</name>
<url>http://47.99.69.84:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>User Project SNAPSHOTS</name>
<url>http://47.99.69.84:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
测试
- 新建两个 setting.xml 文件,文件配置不用的路径,用于测试观察
- 用idea 创建一个 maven 工程
配置 pom.xml 上传 jar包
<?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>com.demo</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>demo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
</build>
<distributionManagement>
<repository>
<id>releases</id>
<name>User Project Release</name>
<url>http://47.99.69.84:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>User Project SNAPSHOTS</name>
<url>http://47.99.69.84:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
- 按照步骤上传 jar 包
- 观察 nexus 服务器 是否上传成功。SNAPSHOTS 版本会自动加时间戳,没有任何影响
- 用idea 再建一个 maven 工程
配置 pom.xml 拉取 demo 。 不同工程 ( pom / war / jar ) 拉取配置不一样。主要在于 type 不一样。
父工程自动拉取。
war 包 则 配置
<type>war</type>
默认拉取 jar 包
<?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>com.getdemo</groupId>
<artifactId>getdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>getdemo Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.demo</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.taotao</groupId>
<artifactId>taotao-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>bantu-framework</groupId>
<artifactId>bantu-im</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<finalName>getdemo</finalName>
</build>
</project>
- 观察拉取效果
jar 包的拉取效果
war 包的拉取效果
pom 父工程包的拉取效果
PS :拉取的时候,我中间纠结了很久,最后发现是 maven setting.xml 文件中的标签写错了,导致没有拉取成功,但是不影响上传。所以,如果看这篇文章没有拉取成功,可以研读 maven setting.xml 的标签含义及拼写,对应检查即可。
资源下载
安装和调试
注意点
成果截图说明
-
整体效果
-
增加阿里云代理配置
-
对 public 进行排序
- 手动上传 jar
错误码
- 401 账号密码错误