美文网首页
Maven私服Nexus搭建和使用

Maven私服Nexus搭建和使用

作者: redexpress | 来源:发表于2018-09-21 11:27 被阅读33次

    一、环境准备

    安装JDK

    步骤略

    二、安装Nexus 服务器

    https://www.sonatype.com/nexus-repository-oss下载Nexus Repository OSS。
    我下载的版本是nexus-3.13.0-01,进入目录nexus-3.13.0-01/bin目录,执行

    ./nexus start
    

    即可启动Nexus
    Nexus 默认的端口是8081,如果要更改端口可以修改etc/nexus-default.properties文件。
    Nexus 管理管理员账户为admin,默认密码为admin123

    三、Maven 和 Gradle 客户端配置

    现在使用Maven和Gradle做构建工具都很普遍,下面分别说明两个工具的使用。

    1、Maven配置

    修改settings.xml 文件

    Maven配置文件更改,修改<Maven安装目录>/conf/settings.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">
      <pluginGroups>
      </pluginGroups>
      <proxies>
      </proxies>
      <servers>
        <server>
          <id>nexus-releases</id>
          <username>admin</username>
          <password>admin123</password>
        </server>
        <server>
          <id>nexus-snapshots</id>
          <username>admin</username>
          <password>admin123</password>
        </server>
      </servers>
      <mirrors>
        <mirror> 
          <id>nexus-public</id> 
          <mirrorOf>*</mirrorOf> 
          <url>http://localhost:8081/repository/maven-public</url> 
        </mirror>
      </mirrors>
    </settings>
    

    Maven项目的pom.xml配置

    <?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>io.github.redexpress</groupId>
      <artifactId>demo</artifactId>
      <version>1.0</version>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
      </properties>
      <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
      </dependencies>
      <distributionManagement>
          <repository>
              <id>nexus-releases</id>
              <url>http://localhost:8081/repository/maven-releases</url>
              <uniqueVersion>true</uniqueVersion>
          </repository>
          <snapshotRepository>
              <id>nexus-snapshots</id>
              <url>http://localhost:8081/repository/maven-snapshots</url>
          </snapshotRepository>
      </distributionManagement>
    </project>
    

    使用mvn deploy命令即可发布jar到Nexus 私服。

    2、Gradle 配置

    build.gradle

    apply plugin: 'java'
    apply plugin: 'maven-publish'
    
    group = 'io.github.redexpress'
    version = '1.0'
    
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    
    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }
    
    repositories {
         maven { url "http://repo.maven.apache.org/maven2" }
         maven { url "http://localhost:8081/repository/maven-public" }
    }
    
    publishing {
        repositories {
            maven {
                credentials {
                    username 'admin'
                    password 'admin123'
                }
                def releasesRepoUrl = 'http://localhost:8081/repository/maven-releases'
                def snapshotsRepoUrl = 'http://localhost:8081/repository/maven-snapshots'
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
            }
        }
        publications {
            maven(MavenPublication) {
                from components.java
            }
        }
    }
    
    dependencies {
        testCompile 'junit:junit:4.12'
    }
    

    settings.gradle

    rootProject.name = 'demo'
    

    相关文章

    Maven私服Archiva搭建和使用

    相关文章

      网友评论

          本文标题:Maven私服Nexus搭建和使用

          本文链接:https://www.haomeiwen.com/subject/ykkanftx.html