1. Background
Recently, some self-made toolkits need to be extracted separately 在打包的时候,同时上传到自己的maven私服仓库,供别人引用,并且还能够引用的时候看到源码
. However, in the process of uploading, it always fails and fails, especially getting angry. In the end, I finally succeeded, and recorded the steps, hoping to help those in need.
2. Steps
Let me talk about my requirements first:
- Local packaging can automatically upload maven private server warehouse.
- After being quoted by others, you can see the source code and comments.
2.1 Modify pom.xml
2.1.1 Specify the upload warehouse address
pom.xml
First, you need to add the following paragraph to your project
<distributionManagement>
<!--稳定版本的仓库地址,必须是允许上传的私服地址-->
<repository>
<id>releases</id>
<url>http://maven.aaaaaa.com/nexus/content/repositories/thirdparty</url>
</repository>
<!--开发版本的仓库地址,必须是允许上传的私服地址-->
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://maven.aaaaaa.com/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
For our usual project version number is similar to XX-SNAPSHOT
this, this type is the data development version, which will be 版本号+时间戳
incremented in the form of after uploading to the private server, and it must specify <snapshotRepository>
the address for uploading.
In the picture above, id
it will be said later that the address here url
corresponds to the address of your warehouse. You can visit a similar address in the browser http://maven.aaaaaa.com/nexus
, enter the account password, and log in. 4
The address corresponding to the picture below is:
It should be noted that , because in the picture above, we have several warehouses. I uploaded them to the 3
corresponding warehouses. You can follow your actual warehouse address.
In addition, id
the corresponding one releases
actually corresponds to the specified maven
configuration file conf/setttings.xml
, as shown in the figure below: If the corresponding label is not configured in
the corresponding one , it also needs to be added. For example:settings.xml
<servers>
<servers>
<server>
<!--与2.1.1中的id值对应-->
<id>releases</id>
<!--账号密码需要与私服登录账号密码一致-->
<username>admin</username>
<password>znxd</password>
</server>
</servers>
Also, note that the account password of the warehouse needs to be correct.
2.1.2 Add source code plugin
The above configuration is only to specify the address of the warehouse, because people who download dependencies need to be able to see the source code, so there is also a need for a plug-in, maven-source-plugin
. Find the project again pom.xml
, add the following plugin:
<!-- 上传源码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Finally, summarize pom.xml
the location of these two new things now:
<project>
<!-----------省略多余的依赖---------->
<build>
<plugins>
<!-----------省略多余的plugin---------->
<!-- 上传源码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://maven.aaaaa.com/nexus/content/repositories/thirdparty</url>
</repository>
</distributionManagement>
</project>
Note: spring-boot-maven-plugin
Plug-ins cannot be referenced. Once this plug-in takes effect, it means that the jar package you are currently making is a runnable package, not the toolkit we uploaded to the private server.
2.2 Command Execution
After the configuration is complete, you can execute the command, which can be realized by two commands, one is idea
executed in the development tool, and the other is the command through the environment variable mvn
.
Note that no matter which of the following commands you use, you need to pay attention to the version number first. If your version of the package exists in the private server, it will definitely fail to package and upload, and an error may be reported: Return code is: 400, ReasonPhrase: Bad Request
.
Therefore, don't forget to change the version. As shown below:
2.2.1 Execution in idea
If your development tool is idea
, in fact, eclipse
the same, it is recommended to use this method.
In the development tool, find the one on the right maven project
, as shown in the figure below:
the final command is actually executed clean deploy
. If the following log appears after the execution, it means that it is successful.
[INFO] Building jar:*******
[INFO] Installing **** to *****
[INFO] --- **** Uploading:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Of course, you can also visit it through a browser to find out whether there is a corresponding jar package in the warehouse.
2.2.2 mvn command execution
If you want to use mvn
commands, you need to pay attention to the configuration of maven environment variables. I won’t talk about how to configure mvn commands here. Here are the steps and points to pay attention to.
First, open the directory where the project is located, and open the current directory through `cmd, as shown in the following figure:
Execute the command as follows:
mvn -s "C:\Program Files\apache-maven-3.5.011\conf\settings-154waiwang.xml" deploy
where -s
is --settings
an abbreviation for .
I was also pitted here. I always thought that because I maven
specified maven_home
/ when configuring the environment variable m2_home
, I think that when I use mvn
the command, it will automatically use the corresponding settings.xml
file for packaging, and then it is not, so why the above It needs to be -s
specified by passing settings.xml
.
If it is not specified, an error will be reported Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
, which probably means no permission. It seems that if it is not specified, a default file will be used, because there is no corresponding account password in that file, so an error will be reported.
2.3 Dependency after success
After the upload is successful, you can find it through the address, as shown in the following figure, it means success:
3. Expansion
For example, if you make this dependency package, there may be an update later, but if the previous dependency package has been used by others, it is impossible to notify users one by one. Maven itself provides this function of automatically downloading higher versions. Need to version
change the value of the label. as follows:
<dependency>
<groupId>com.agri</groupId>
<artifactId>znxd-framework</artifactId>
<version>[1.0,)</version>
</dependency>
where it [1.0,)
means update 1.0
the latest package starting from version. This has the advantage that when your extension package is updated to a larger 1.0
package, the user's project will automatically change to the latest package. In this way, when you write wrong code, you can secretly update to the latest package by yourself to avoid embarrassment.