【Maven】---Nexus私服配置Setting和Pom

maven---nexus私服配置setting和pom

上一遍博客已经在linux服务器上,搭建好nexus私服了,博客地址:Linux搭建Nexus3.X私服

现在就需要配置setting.xmlpom.xml来使nexus作为maven的私服。setting.xml文件在conf下面,pom.xml是在你创建maven项目中的pom.xml中。

一、将jar发送到nexus私服务器

1、创建maven项目

创建一个最简单的maven项目,然后新建一个工具类,用来测试当把它打成jar包放到私服后,其它项目是否能够成功引用。
1090617-20181018100113637-48403017.jpg

2、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>com.jincou</groupId>
    <artifactId>xuxiaoxiao</artifactId>
    <!--SNAPSHOT代表是快照版本-->
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>xuxiaoxiao</name>
    <description>Demo project</description>

    <distributionManagement>
        <repository>
            <!--id的名字可以任意取,但是在setting文件中的属性<server>的ID与这里一致-->
            <id>releases</id>
            <!--指向仓库类型为host(宿主仓库)的储存类型为Release的仓库-->
            <url>http://47.96.4.110:8081/repository/java-release/</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <!--指向仓库类型为host(宿主仓库)的储存类型为Snapshot的仓库-->
            <url>http://47.96.4.110:8081/repository/java-snapshot/</url>
        </snapshotRepository>
    </distributionManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    
</project>
3、setting.xml配置

在这里只要配置登陆nexus的用户名密码,不然没有用户名和密码怎么能将jar包发送到私服呢。

<!--此处设置的用户名和密码都是nexus的登陆配置-->
 <servers>
     <server>
      <id>releases</id>  <!--对应pom.xml的id=releases的仓库-->
      <username>xuxiaoxiao</username>
      <password>xuxiaoxiao123</password>
    </server>
     <server>
      <id>snapshots</id> <!--对应pom.xml中id=snapshots的仓库-->
      <username>xuxiaoxiao</username>
      <password>xuxiaoxiao123</password>
    </server>
  </servers>

注意maven会判断版本后面是否带了-SNAPSHOT,如果带了就发布到snapshots仓库,否则发布到release仓库。这里我们可以在pom.xml文件中

执行命令:mvn deploy

1090617-20181018100419879-764869951.png

发现部署到nexus私服成功,我们到私服查看下,因为这里的版本是带SNAPSHOT,所以会发布到snapshots仓库中。
1090617-20181018100434484-1373637185.jpg
说明已经成功将jar包发布到nexus私服中了。那么下一步是如何引用私服中的jar包了。

二、从nexus引用第三方jar包

让maven项目使用nexus作为远程仓库有两种方式,第一种是在项目的pom.xml中进行更改,让单个项目使用nexus仓库;另一种是通过修改maven的配置文件settings.xml进行更改,让所有项目都使用nexus仓库。我们这里采取第二种,只需要setting.xml就可以了。还有就是拉取jar的私服仓库地址只要写一个java-group就可以了,因为在创建这个组的时候,里面已经包含了其它三个仓库。

1、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">
    <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>

 <servers>
    <!--第一个nexus-xu要和下面的mirror中的id一致,代表拉取是也需要进行身份校验-->
    <server>
      <id>nexus-xu</id>
      <username>xuxiaoxiao</username>
      <password>xuxiaoxiao113</password>
    </server>
     <server>
      <!--这两个前面讲过,是jar上传时候进行的验证,id对应的是pom中id属性的值-->
      <id>releases</id>
      <username>xuxiaoxiao</username>
      <password>xuxiaoxiao113</password>
    </server>
     <server>
      <id>snapshots</id>
      <username>xuxiaoxiao</username>
      <password>xuxiaoxiao113</password>
    </server>
  </servers>

  <mirrors>
    <mirror>
        <id>nexus-xu</id>
        <name>internal nexus repository</name>
        <!--镜像采用配置好的组的地址-->
        <url>http://47.96.44.110:8081/repository/java-group/</url>
        <mirrorOf>!internal.repo,*</mirrorOf>
    </mirror>
  </mirrors>

  <profiles>
<profile>
  <!--ID用来确定该profile的唯一标识-->
           <id>jdk-1.8</id>
           <activation>
               <activeByDefault>true</activeByDefault>
               <jdk>1.8</jdk>
           </activation>
           <properties>
               <maven.compiler.source>1.8</maven.compiler.source>
               <maven.compiler.target>1.8</maven.compiler.target>
               <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
           </properties>
   </profile>

    <profile>
  <id>nexus-pr</id>
   <!-- 远程仓库列表 -->
  <repositories>
    <repository>
      <id>nexus-xu</id>
      <name>Nexus Central</name>
     <!-- 虚拟的URL形式,指向镜像的URL-->
      <url>http://47.96.44.110:8081/repository/java-group/</url>
      <layout>default</layout>
     <!-- 表示可以从这个仓库下载releases版本的构件-->  
      <releases>
        <enabled>true</enabled>
      </releases>
     <!-- 表示可以从这个仓库下载snapshot版本的构件 -->  
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>
   <!-- 插件仓库列表 -->
  <pluginRepositories>
    <pluginRepository>
      <id>nexus-xu</id>
      <name>Nexus Central</name>
      <url>http://47.96.44.110:8081/repository/java-group/</url>
      <layout>default</layout>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <releases>
         <enabled>true</enabled>
      </releases>
    </pluginRepository>
  </pluginRepositories>
   </profile>
  </profiles>

  <activeProfiles>
     <!--需要激活 <profile>中的ID才生效-->  
    <activeProfile>nexus-pr</activeProfile>
    <activeProfile>jdk-1.8</activeProfile>
  </activeProfiles>
</settings>
2、验证

(1)新建项目添加pom依赖

    <dependencies>
        <dependency>
            <groupId>com.jincou</groupId>
            <artifactId>xuxiaoxiao</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

(2)看是否拉取到私服的jar包
1090617-20181018100459596-188794300.jpg

并没有报错,表拉取成功

(3)写测试类
1090617-20181018100511296-838616878.jpg

引用成功

(4)看后台输出
1090617-20181018100529241-134165830.jpg

输出成功

从这里将jar包发送到私服和从私服拉取jar就成功了。

参考

1、maven发布jar包到nexus
2、让Maven项目使用Nexus作为远程仓库的settings.xml配置
3、Maven 全局配置文件settings.xml详解

如果一个人充满快乐,正面的思想,那么好的人事物就会和他共鸣,而且被他吸引过来。同样,一个人老带悲伤,倒霉的事情也会跟过来。

                                                  ——在自己心情低落的时候,告诫自己不要把负能量带给别人。(大校2)

转载于:https://www.cnblogs.com/qdhxhz/p/9808642.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Maven Assembly Plugin 可以用来将 Maven 项目打包成一个可执行的 JAR 或者 WAR,或者一个包含所有依赖的 ZIP 文件。下面是一个 pom.xml 文件中配置 Maven Assembly Plugin 的示例: ```xml <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.example.Main</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 在上面的配置中,我们指定了 Maven Assembly Plugin 的版本号为 3.1.0。在 configuration 标签中,我们指定了使用 jar-with-dependencies 描述符,这会将项目打包成一个包含所有依赖的 JAR 文件。在 archive 标签中,我们指定了打包后的 JAR 文件中的主类为 com.example.Main。在 executions 标签中,我们指定了需要在 package 阶段执行 Maven Assembly Plugin。最后,在 plugin 标签中,我们指定了 Maven Assembly Plugin 的 artifactId 为 maven-assembly-plugin。 ### 回答2: pom 文件是 Maven 项目的配置文件,能够定义项目的依赖关系和插件配置等信息。Maven-assembly-plugin 是一个 Maven 插件,用于将项目打包成可执行的应用程序。 在 pom 文件中配置 Maven-assembly-plugin 需要在 `<build>` 标签下添加如下代码段: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 上述代码中的 `<configuration>` 标签指定了使用 `jar-with-dependencies` 描述符来打包项目,该描述符会将项目的所有依赖打包到一个单独的可执行的 JAR 文件中。 `<executions>` 标签中的 `<execution>` 子标签定义了插件在打包(package)阶段执行的行为。`<id>` 指定了该执行的唯一标识符,`<phase>` 指定了执行的阶段,这里是在项目打包阶段执行,`<goals>` 指定了要执行的目标,这里是执行单一目标(single)。 通过上述配置,当我们运行 Maven 的 `package` 命令时,Maven-assembly-plugin 将会按照配置的描述符将项目及其所有依赖打包成一个可执行的 JAR 文件。 ### 回答3: pom 文件的配置是用来指定项目的构建和打包方式的。Maven-assembly-plugin 是 Maven 中用来打包应用程序的插件之一。 在 pom 文件中配置 Maven-assembly-plugin 时,首先需要在 `<build><plugins>` 标签中添加插件的声明,如下: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>版本号</version> <configuration> <!-- 插件的配置代码 --> </configuration> <executions> <execution> <phase>所需的构建阶段</phase> <goals> <goal>所需的目标</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 在 `<configuration>` 标签中,我们可以配置打包的方式、打包的内容等。常见的配置包括 `<descriptors>`、`<formats>`、`<includes>` 和 `<excludes>` 等。例如: ```xml <configuration> <descriptors> <descriptor>所需的 descriptor 文件路径</descriptor> </descriptors> <formats> <format>所需的打包格式</format> </formats> <includes> <include>所需包含的文件或目录</include> </includes> <excludes> <exclude>所需排除的文件或目录</exclude> </excludes> <!-- 其他配置项 --> </configuration> ``` 配置完插件后,需要在 `<executions>` 标签中指定插件执行的阶段和目标。通常,我们会将 Maven-assembly-plugin 配置在 `package` 阶段中执行,以便在打包应用程序时同时生成相应的分发包。例如: ```xml <execution> <phase>package</phase> <goals> <goal>所需的目标</goal> </goals> </execution> ``` 以上是关于如何在 pom 文件中配置 Maven-assembly-plugin 的简单介绍。通过正确的配置,我们可以根据需求生成符合特定格式的打包文件,方便部署和分发应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值