关于maven:图文并茂做开发这么久了还不会搭建服务器Maven私有仓库这也太Low了吧

5次阅读

共计 6745 个字符,预计需要花费 17 分钟才能阅读完成。

大家好,我是冰河~~

最近不少小伙伴想在本人公司的内网搭建一套 Maven 私服环境,可本人搭建的过程中,或多过少的总会呈现一些问题,问我可不可以出一篇如何搭建 Maven 私服的文章。这不,就有了这篇文章嘛。

好了,其余的不多说了,接下来,咱们就一起来搭建 Maven 私服环境吧!

环境阐明

  • 环境:CentOS 6.x~8.0、JDK8、Sonatype Nexus、Maven
  • IP:192.168.50.131
  • root 用户操作

装置 Nexus

前提:已装置 JDK8 并配置好了环境变量,小伙伴们自行搭建 JDK8 环境,这里我就不再赘述了。置信小伙伴们都可能正确搭建 JDK8 环境。

下载 Nexus

下载 Nexus(这里,我应用的是:nexus-2.11.2-03-bundle.tar.gz), 下载地址:http://www.sonatype.org/nexus…,咱们也能够在服务器的命令行输出如下命令下载 nexus-2.11.2-03-bundle.tar.gz 安装文件。

# wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.2-03-bundle.tar.gz

也能够到链接:https://download.csdn.net/dow… 下载

解压 Nexus

# mkdir nexus
# tar -zxvf nexus-2.11.2-03-bundle.tar.gz -C nexus
# cd nexus
# ls
nexus-2.11.2-03 sonatype-work
(一个 nexus 服务,一个公有库目录)

编辑 Nexus

编辑 Nexus 的 nexus.properties 文件,配置端口和 work 目录信息(保留默认)

# cd nexus-2.11.2-03
# ls
bin conf lib LICENSE.txt logs nexus NOTICE.txt tmp

查看目录构造,jetty 运行

# cd conf
# vi nexus.properties
# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus
# Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus
runtime=${bundleBasedir}/nexus/WEB-INF

编辑 nexus 脚本,配置 RUN_AS_USER 参数

# vi /usr/local/nexus/nexus-2.11.2-03/bin/nexus
#RUN_AS_USER=

改为:

RUN_AS_USER=root

防火墙中关上 8081 端口

# vi /etc/sysconfig/iptables

增加:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT

保留后重启防火墙

# service iptables restart

启动 nexus

# /usr/local/nexus/nexus-2.11.2-03/bin/nexus start
****************************************
WARNING - NOT RECOMMENDED TO RUN AS usr/local
****************************************
Starting Nexus OSS...
Started Nexus OSS.

拜访 nexus

浏览器中关上:http://192.168.50.131:8081/ne…

登录 nexus

默认用户名 admin, 默认明码 admin123。

到此,Nexus 已装置实现,接下来是 Nexus 的配置

Nexus 配置(登录后)

设置管理员邮箱

菜单 Administration/Server 配置邮箱服务地址(如果遗记明码,能够通过该邮箱找回明码)

设置用户邮箱

给用户配置邮箱地址,不便遗记明码时找回:

用户批改明码

仓库类型

  • group 仓库组:Nexus 通过仓库组的概念对立治理多个仓库,这样咱们在我的项目中间接申请仓库组即可申请到仓库组治理的多个仓库;
  • hosted 宿主仓库:次要用于公布外部我的项目构件或第三方的我的项目构件(如购买商业的构件)以及无奈从公共仓库获取的构件(如 oracle 的 JDBC 驱动)proxy 代理仓库:代理公共的近程仓库;
  • virtual 虚构仓库:用于适配 Maven 1;

个别用到的仓库品种是 hosted、proxy。

Hosted 仓库罕用类型阐明:

  • releases 外部的模块中 release 模块的公布仓库
  • snapshots 公布外部的 SNAPSHOT 模块的仓库
  • 3rd party 第三方依赖的仓库,这个数据通常是由内部人员自行下载之后公布下来

如果构建的 Maven 我的项目本地仓库没有对应的依赖包,那么就会去 Nexus 私服去下载,如果 Nexus 私服也没有此依赖包,就回去近程地方仓库下载依赖,这些地方仓库就是 proxy。Nexus 私服下载胜利后再下载至本地 Maven 库供我的项目援用。

设置 proxy 代理仓库

设置 proxy 代理仓库 (Apache Snapshots/Central/Codehaus Snapshots) 准许近程下载,如下所示。

Maven 本地库的装置与配置

下载 Maven

到链接 http://maven.apache.org/downl… 下载 Maven

配置 Maven 环境变量

vim /etc/profile

MAVEN_HOME=/usr/local/maven
JAVA_HOME=/usr/local/jdk
CLASS_PATH=$JAVA_HOME/lib
PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH
export JAVA_HOME MAVEN_HOME CLASS_PATH PATH

source /etc/profile

配置本地 Maven

拷贝 Maven 的 conf 目录下的配置文件 settings.xml,重命名为 settings-lyz.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>D:/Maven_Repository/.m2/repository</localRepository>
 <interactiveMode>true</interactiveMode>
    <offline>false</offline>
    <pluginGroups>
        <pluginGroup>org.mortbay.jetty</pluginGroup>
        <pluginGroup>org.jenkins-ci.tools</pluginGroup>
    </pluginGroups>
 
 <!-- 配置权限, 应用默认用户 -->
 <servers>
  <server>
   <id>nexus-releases</id>
   <username>deployment</username>
   <password>deployment123</password>
  </server>
  <server> 
   <id>nexus-snapshots</id>
   <username>deployment</username>
   <password>deployment123</password>
  </server>
 </servers>
 
    <mirrors>
 
    </mirrors>
 
 <profiles>
  <profile>
     <id>lyz</id>
       <activation>
                    <activeByDefault>false</activeByDefault>
                    <jdk>1.8</jdk>
                </activation>
       <repositories>
     <!-- 公有库地址 -->
        <repository>
      <id>nexus</id>
      <url>http://192.168.50.131:8081/nexus/content/groups/public/</url>
      <releases>
       <enabled>true</enabled>
      </releases>
      <snapshots>
       <enabled>true</enabled>
      </snapshots>
     </repository>
    </repositories>      
    <pluginRepositories>
     <!-- 插件库地址 -->
     <pluginRepository>
      <id>nexus</id>
      <url>http://192.168.50.131:8081/nexus/content/groups/public/</url>
      <releases>
       <enabled>true</enabled>
      </releases>
      <snapshots>
       <enabled>true</enabled>
        </snapshots>
     </pluginRepository>
    </pluginRepositories>
   </profile>
 </profiles>
 
 <!-- 激活 profile-->
 <activeProfiles>
  <activeProfile>lyz</activeProfile>
 </activeProfiles>

</settings>

其中,配置文件中的

<localRepository>D:/Maven_Repository/.m2/repository</localRepository>

阐明本地仓库位于 D:/Maven_Repository/.m2/repository 目录下。

配置文件中的如下配置项。

<url>http://192.168.50.131:8081/nexus/content/groups/public/</url>

与下图中的链接统一:

配置 Eclipse Maven

配置 IDEA Maven

我的项目的构建与公布

首先咱们要在我的项目的 pom.xml 文件中退出如下内容,将我的项目构建成的 Jar 公布到 Maven 公有仓库

<distributionManagement>
    <repository>
        <id>nexus-releases</id>
        <name>Nexus Release Repository</name>
        <url>http://192.168.50.131:8081/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <name>Nexus Snapshot Repository</name>
        <url>http://192.168.50.131:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>

配置阐明

我的项目中的 pom.xml 文件中,如果版本配置如下:

<version>0.0.1-SNAPSHOT</version>

则公布到 Maven 公有仓库后对应的目录如下:

如果版本配置如下:

<version>0.0.1-RELEASE</version>

则公布到 Maven 公有仓库后对应的目录如下。

残缺 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>common-utils-maven</groupId>
    <artifactId>com.chwl.common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://192.168.50.131:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://192.168.50.131:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.8</jdk.version>
    </properties>
    <dependencies>
       此处省略....
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.2</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

具体公布步骤如下:

右键 pom.xml->Run as->Maven build->

上图中的公有库为空,咱们右键 pom.xml->Run as->Maven build(此时 pom.xml 文件的 version 为 0.0.1-SNAPSHOT)。

构建结束后

阐明曾经将我的项目构建并公布到了咱们的 Maven 公有仓库。

此时,上图中的 Release 目录为空,此时,咱们批改 pom.xml 的 version 为 0.0.1-RELEASE,再次右键 pom.xml->Run as->Maven build,构建我的项目,此时公布的目录如下图:

阐明曾经将我的项目构建并公布到了咱们的 Maven 公有仓库。

最初,咱们增加第三方的 Jar 依赖到咱们的 Maven 公有仓库,具体操作如下:

如上图,第三方依赖公有仓库为空, 咱们依照以下步骤上传第三方依赖到咱们的 Maven 公有仓库。

如上图,第三方依赖曾经上传到咱们的 Maven 公有仓库。

至此,Maven 公有库和本地库的装置与配置到此结束。

好了,明天就到这儿吧,我是冰河,咱们下期见~~

正文完
 0