Maven项目打包jar到私服

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010956470/article/details/71080622

工具:IntelliJ IDEA、Maven
前提 已经有私服地址(这里假设为 http://192.168.1.112:8081/nexus/),并且maven已经做好相关的配置
maven中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">

  <localRepository>D:\repo</localRepository>
  <mirrors>
    <mirror>
      <id>MyCentral</id>
      <name>nexus</name>
      <url>http://192.168.1.112:8081/nexus/content/repositories/central/</url>
      <mirrorOf>central</mirrorOf>        
    </mirror>   
  </mirrors>
  <profiles>
  <profile>
      <id>MyCentral</id>
      <repositories>
        <repository>
          <id>Mythirdparty</id>
          <name>SmartCentury thirdparty repository</name>
          <url>http://192.168.1.112:8081/nexus/content/repositories/thirdparty/</url>
        </repository>
        <repository>
          <id>My-Central</id>
          <name>SmartCentury central repository</name>
          <url>http://192.168.1.112:8081/nexus/content/repositories/central/</url>
            <releases><enabled>true</enabled></releases> 
            <snapshots><enabled>true</enabled></snapshots> 
        </repository>
        <repository>
            <id>nexus-releases</id>
            <name>nexus-releases</name>
            <url>http://192.168.1.112:8081/nexus/content/repositories/releases/</url>
        </repository>
        <repository>
            <id>nexus-snapshot</id>
            <name>nexus-snapshot</name>
            <url>http://192.168.1.112:8081/nexus/content/repositories/snapshots/</url>
        </repository>
      </repositories>

    <pluginRepositories> 
      <pluginRepository> 
        <id>MyPlugin</id> 
        <name>Nexus</name> 
        <url>>http://192.168.1.112:8081/nexus/content/repositories/central/</url> 
        <releases><enabled>true</enabled></releases> 
        <snapshots><enabled>true</enabled></snapshots> 
      </pluginRepository> 
   </pluginRepositories>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>MyCentral</activeProfile>
  </activeProfiles>
</settings>

1、在Maven中settings.xml配置文件中的servers节点中配置如下:

<servers>
    <server>
      <id>nexus-snapshot</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-release</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
</servers>

2、在要打包到私服的maven项目的pom.xml文件中,添加如下配置:

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Packaging Release Repository</name>
            <url>http://192.168.1.209:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshot</id>
            <name>Packaging Snapshot Repository</name>
            <url>http://192.168.1.209:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

注:这里的id与settings文件中server的id相对应;maven-source-plugin是为了将源代码可以打包打私服上。

3、如果没有maven,则全部要在pom.xml中配置。还要配置相关的maven plugin

<properties>
        <file_encoding>UTF-8</file_encoding>
        <project.build.sourceEncoding>${file_encoding}</project.build.sourceEncoding>
        <java_source_version>1.7</java_source_version>
        <java_target_version>1.7</java_target_version>
        <!-- maven插件 -->
        <maven_clean_plugin_version>2.5</maven_clean_plugin_version>
        <maven_resources_plugin_version>2.6</maven_resources_plugin_version>
        <maven_compiler_plugin_version>3.1</maven_compiler_plugin_version>
        <maven_source_plugin_version>3.0.1</maven_source_plugin_version>
        <maven_jar_plugin_version>2.3.2</maven_jar_plugin_version>
        <maven_war_plugin_version>2.1.1</maven_war_plugin_version>
        <maven_install_plugin_version>2.5.1</maven_install_plugin_version>
        <maven_deploy_plugin_version>2.7</maven_deploy_plugin_version>
        <maven_surefire_plugin_version>2.17</maven_surefire_plugin_version>
    </properties>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>${maven_clean_plugin_version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>${maven_resources_plugin_version}</version>
                    <configuration>
                        <encoding>${file_encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven_compiler_plugin_version}</version>
                    <configuration>
                        <fork>true</fork>
                        <source>${java_source_version}</source>
                        <target>${java_target_version}</target>
                        <encoding>${file_encoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>${maven_source_plugin_version}</version>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <phase>package</phase>
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>${maven_jar_plugin_version}</version>
                    <configuration>
                        <archive>
                            <addMavenDescriptor>true</addMavenDescriptor>
                            <index>true</index>
                            <manifest>
                                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>${maven_war_plugin_version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>${maven_install_plugin_version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>${maven_deploy_plugin_version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven_surefire_plugin_version}</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>nexus-releases</id>
            <name>nexus-releases</name>
            <url>http://192.168.1.112:8081/nexus/content/repositories/releases/</url>
            <releases><enabled>true</enabled></releases>
        </repository>
        <repository>
            <id>nexus-snapshot</id>
            <name>nexus-snapshot</name>
            <url>http://192.168.1.112:8081/nexus/content/repositories/snapshots/</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
    </repositories>
    <!-- 部署到nexus私服上的配置 -->
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>nexus-releases</name>
            <url>http://192.168.1.112:8081/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshot</id>
            <name>nexus-snapshot</name>
            <url>http://192.168.1.112:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

4、输入 mvn deploy 命令,即可将jar包和source文件一并上传到私服上。

猜你喜欢

转载自blog.csdn.net/u010956470/article/details/71080622