Maven+Nexus for SCM release

    For some public dependency packages of java, we usually need to publish them in the private nexus platform for other project groups to use.

    1) We first need to build our own nexus platform.

    2) User authorization, the specified user has the release authority.

    3) Adjust the setting.xml file in maven.

    4) Adjust the pom.xml file in the project.

 

1. Setting.xml configuration example:

 

<?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>releases</id>
      <username>admin</username>
      <password>admin</password>
    </server>
	<server>
      <id>snapshots</id>
      <username>admin</username>
      <password>admin</password>
    </server>
  </servers>

  <mirrors>
  </mirrors>

 <profiles>
    	<profile>
        	<id>nexus</id>
        	<repositories>
            	<repository>
                	<id>nexus_public</id>
                	<name>Team Repository</name>
                	<url>http://nexus.test.com.cn/nexus/content/groups/public/</url>
                	<releases>
                    	<enabled>true</enabled>
                	</releases>
                	<snapshots>
                   		<enabled>true></enabled>
                   		<updatePolicy>always</updatePolicy>
                		<checksumPolicy>fail</checksumPolicy>
                	</snapshots>
            	</repository>
            	<repository>
            		<id>snapshots</id>
            		<url>http://nexus.test.com.cn/nexus/content/repositories/snapshots</url>
        		</repository>
        	</repositories>

        	<pluginRepositories>
            	<pluginRepository>
                	<id>nexus_public</id>
            		<url>http://nexus.test.com.cn/nexus/content/groups/public/</url>
            		<releases>
                		<enabled>true</enabled>
            		</releases>
            		<snapshots>
                		<enabled>true</enabled>
            		</snapshots>
            	</pluginRepository>
            	<pluginRepository>
            		<id>central</id>
            		<name>Central Repository</name>
            		<url>https://repo.maven.apache.org/maven2</url>
            		<releases>
                		<updatePolicy>never</updatePolicy>
            		</releases>
            		<snapshots>
                		<enabled>false</enabled>
            		</snapshots>
        		</pluginRepository>
        	</pluginRepositories>
    	</profile>
    </profiles>
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
</settings>

 

    Configure the nexus server-side user authorization information in the <server> tag. Usually, we need to configure two: releases and snapshots. Of course, if your jar does not need to be released for snapshots, you can also configure it. The way to access the nexus server when referencing the jar in the project is configured in <profile>.

 

2, pom.xml sample

 

<?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>
    <description>jdbc utils</description>

    <groupId>com.test.utils</groupId>
    <artifactId>jdbc-utils</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>jdbc-utils</name>

    <properties>
		<jedis.version />
        <java.version>1.8</java.version>
        <org.springframework-version>4.3.3.RELEASE</org.springframework-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <scm>
        <developerConnection>scm:git:ssh://[email protected]:3590/yourname/jdbc-utils.git</developerConnection>
        <url>scm:git:ssh://[email protected]:3590/yourname/jdbc-utils.git</url>
        <connection>scm:git:ssh://[email protected]:3590/yourname/jdbc-utils.git</connection>
	<!-- The tag name is generated on this git address, and you will be prompted for input when release:prepare, you can configure it here -->
        <tag>jdbc-utils-1.0</tag>
    </scm>
    <distributionManagement>
        <!-- if you use public-release -->
        <repository>
            <id>releases</id>
            <name>releases</name>
            <url>http://nexus.test.com.cn/nexus/content/repositories/releases/</url>
        </repository>
        <!-- if you use snapshots. -->
        <snapshotRepository>
            <id>snapshots</id>
            <name>snapshots</name>
            <url>http://nexus.test.com.cn/nexus/content/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.3</version>
            </plugin>
        </plugins>
    </build>
</project>

 

    1) The version of the project needs to end with "-SNAPSHOT", otherwise it will throw when releasing:

You don't have a SNAPSHOT project in the reactor projects list.

 

    2) The "maven-release-plugin" needs to be configured in the project, otherwise the command cannot be executed, and the version of this plugin should preferably be the latest and compatible with the maven version.

    3) Configure your git address in the <scm> tag, and have permissions such as "push" for this git address.

    4) In the <distributionManagement> tag, configure the address for code release, including "releases" and "snapshots" (optional). The value of <id> must correspond to <server>.<id> in setting.xml. It is important to note here that the user configured in <server> needs to have code submission permissions for the two addresses in pom.xml. Otherwise it will throw:

Return code is: 401, ReasonPhrase: Unauthorized.

 

    5) In order to avoid unnecessary problems, we also need to modify the access permissions of the corresponding two urls in pom.xml as shown below (on the nexus platform):

 

 

    If not modified, it will most likely throw:

Return code is: 400, ReasonPhrase: Bad Request.

 

3. Execute:

    For SCM releases, we usually focus on three directives (see maven-release-plugin ):

    1) mvn release:clean: clean up the temporary files generated during the release process, usually when we have an error in any process of release execution, or when we give up the release, execute this command, which is similar to a "rollback" operation.

    2) mvn release:rollback: rollback operation, when a part of the release operation is executed, regardless of whether an error occurs, if you want to roll back to the code state before the release, you can perform this operation. (Note that during the execution of the release:prepare phase, the pom.xml file will be modified, and rollback means rolling back to the state before prepare)

    3) mvn release:prepare: The preparation stage of release is also the first operation of the normal flow. At this stage, the operator will be prompted for the version number of this release, the version number of SNAPSHOT to be developed next, the name of the generated tag, etc. We Usually it is all the way to enter, and all operations can be kept by default.

    4) mvn release:perform: execute release remote release, after successful execution, we should see our jar list in the url of nexus response.

    5) For each release, a tag will be generated on the git address specified by scm. This tag number is consistent with your version. If you make an accidental error, re-release of the same version may not succeed, because this tag already exists, this When you need to execute "git tag -d <your tag name>" to delete the tag to continue. (You can view the list of tags through "git tag")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326123529&siteId=291194637