Maven 项目发布到公司内部 Repository(Nexus)

1 配置结构关系

1.1 项目 pom.xml 配置

1、 先配置发布到哪个 repository 相关信息:

<distributionManagement>
    <repository>
        <id>deployment</id>
        <name>internal repository for releases</name>
        <url>http://192.168.0.1:9000/nexus/content/repositories/releases</url>
    </repository>
    <snapshotRepository>
        <id>deployment</id>
        <name>internal repository for snapshots</name>
        <url>http://192.168.0.1:9000/nexus/content/repositories/snapshots</url>
    </snapshotRepository>
</distributionManagement>

2、配置 deploy 插件:

<plugins>
        <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
        </plugin>
</plugins>

3、顺便把这些 plugin 一起配置,这样可以 jar, source jar, javadoc jar 一起发布:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <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>
        <executions>
            <execution>
                <id>attach-sources</id>
                <goals>
                    <goal>jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <configuration>
            <charset>${file_encoding}</charset>
            <encoding>${file_encoding}</encoding>
        </configuration>
        <executions>
            <execution>
                <id>attach-javadocs</id>
                <goals>
                    <goal>jar</goal>
                </goals>
            </execution>
        </executions>
</plugin>

1.2 settings.xml 配置

Nexus 通常会设置认证, 只允许指定的 credentials 可以发布 artifacts 到 nexus, 而这些认证信息就配置在 settings.xml 中:

<servers>
    <server>
        <id>deployment</id>
        <username>${username}</username>
        <password>${password}</password>
    </server>
</servers>

2 执行发布/部署

在当前项目目录下执行 mvn deploy 即可!