亲宝软件园·资讯

展开

Maven deploy配置 Maven deploy配置方法详解

二师兄-公众号-程序新视界 人气:4

作用

在本地的pom文件配置好之后,执行deploy命令,可以将maven所打的jar包上传到远程的repository,便于其他开发者和工程共享。

pom.xml配置

首选,在pom文件中project标签下添加如下代码:

<distributionManagement> 
  <repository> 
   <id>releases</id> 
   <name>Internal Releases</name> 
   <url>http://localhost:8081/nexus/content/repositories/thirdparty</url> 
  </repository> 
  <snapshotRepository> 
   <id>releases</id> 
   <name>Internal Releases</name> 
   <url>http://localhost:8081/nexus/content/repositories/thirdparty</url> 
  </snapshotRepository>
 </distributionManagement> 

此时,执行deploy命令,会返回401错误,则需要用户验证或验证的信息有误。

setting.xml文件配置

在setting配置文件的servers标签下添加如下代码:

  <server> 
    <id>releases</id> 
    <username>admin</username> 
    <password>admin</password> 
   </server> 

PS:其中此处的id,必须为pom文件中配置的repository的id。

注意事项

一般继承 parent 的version会按照如下格式写:

  <parent> 
    <groupId>module.some</groupId> 
    <artifactId>module_parent</artifactId> 
    <version>${parent.version}</version> 
  </parent> 

这样写方便统一管理版本信息,但发布到maven私服之后,maven 会试图下载 module_parent 的 ${parent.version} 的 jar。显然,这个jar是不存在的。那么为什么已经指定了 parent.version 的值了却没有解析呢?这是因为deploy 的过程中,parent 标签里的变量是不会解析的,必须是一个常量。

结果

执行maven deploy命令成功之后,登录私服进行查询,即可看到对应的jar包。

maven deploy命令打包到私服

<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.zeelan.app</groupId>
  <artifactId>seller-auth</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <!-- 项目编码 -->
  <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <lombok.version>1.16.10</lombok.version>
    <mybatis.paginator.version>1.2.17.2</mybatis.paginator.version>
    <hibernate.validator.version>5.1.2.Final</hibernate.validator.version>
    <validation.api.version>1.1.0.Final</validation.api.version> 
  </properties>

  <!-- 远程仓库地址 -->
  <pluginRepositories>
    <pluginRepository>
      <id>nexus</id>
      <name>Team Nexus Repository</name>
      <url>http://192.168.0.126:8081/nexus/content/groups/public</url>
    </pluginRepository>
  </pluginRepositories>

  <!-- 配置远程发布到私服,mvn deploy -->
  <distributionManagement>
    <!-- 定义releases库的坐标 -->
    <repository>
      <id>releases</id>
      <name>Nexus Release Repository</name>
      <url>http://192.168.0.126:8081/nexus/content/repositories/releases/</url>
    </repository>
    <!-- 定义snapshots库 -->
    <snapshotRepository>
      <id>snapshots</id>
      <name>Nexus Snapshot Repository</name>
      <url>http://192.168.0.126:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
  </distributionManagement>

  <!-- 项目有依赖的所有jar坐标配置 -->
  <dependencies>
    <!-- lombok jar -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>${lombok.version}</version>
    </dependency>

    <!-- mybatis分页插件jar -->
    <dependency>
      <groupId>com.github.miemiedev</groupId>
      <artifactId>mybatis-paginator</artifactId>
      <version>${mybatis.paginator.version}</version>
    </dependency>
    
    <!-- hibernate validator验证jar -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>${hibernate.validator.version}</version>
    </dependency>
    <!-- hibernate validtor需要的依赖 jar -->
    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>${validation.api.version}</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>seller-auth</finalName>
    <!-- 插件库声明 -->
    <plugins>
      <!-- 配置运行环境JDK版本 -->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

      <!-- 将源码打包插件 -->
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <attach>true</attach>
        </configuration>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- deploy时只上传jar包到远程仓库的配置 -->
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <!-- skip默认deploy插件的执行 -->
            <configuration>
              <skip>true</skip>
            </configuration>
          </execution>
          <execution>
            <id>deploy-file</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy-file</goal>
            </goals>
            <configuration>
              <!-- 开发阶段上传到snapshot仓库,上线阶段上传到release仓库 -->
              <repositoryId>${project.distributionManagement.snapshotRepository.id}</repositoryId>
              <url>${project.distributionManagement.snapshotRepository.url}</url>
              <file>${project.build.directory}/${project.artifactId}.jar</file>
              <groupId>${project.groupId}</groupId>
              <artifactId>${project.artifactId}</artifactId>
              <version>${project.version}</version>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

执行 mvn deploy就能打包到私服上了!

mvn -clean配置清除插件,然后在执行命令可以清除target下的文件

mvn-clean package 本地打包,jar/war/等根据<packaging>jar/war</packaging>控制

mvn -e 查看打包过程的错误信息

mvn -v查看mavne版本信息等等

加载全部内容

相关教程
猜你喜欢
用户评论