Maven usage in disconnected environment

Background appeal:

Recently, I helped build a Java development environment in the bank Intranet environment, in which Maven was used for project management. According to the previous understanding, I first set up the framework of the whole project under the external network environment, and then copy the relevant documents, such as project documents, Maven local warehouse, etc., into the computer on the internal network.
Use: Spring Cloud related dependencies.

Question:

In practice, although all files in Maven's local warehouse have been copied into the intranet machine, there are still problems during the operation of the project, resulting in failure to start normally.

Positioning questions:

First, add Maven's relevant knowledge:
1. For conf / setting. In Maven installation file XML file with the mirror tag

    <mirror>
        <id>alimaven</id> //Represents the id of this image
        <mirrorOf>central</mirrorOf> //Indicates which remote public warehouse it is. Here, it indicates the central warehouse
        <name>aliyun maven</name> //Name of this mirror
        <url>http://maven. aliyun. COM / nexus / content / repositories / central / < / url > / / the address of this image
    </mirror>

2. Local warehouse address:

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

Normally, when Maven reads the dependencies in the POM file, it will follow: local warehouse > > remote warehouse specified in setting > > remote warehouse specified in POM file.
3. Offline mode: offline mode determines whether to connect to the Internet when build ing through Maven

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>true</offline>
  -->

According to the Internet, let's try to use offline mode first:
Using Maven offline mode in Idea
Technical picture
Then run in the terminal window:
mvn clean compile -o
The discovery or error is reported to the effect that the information cannot be synchronized from the remote warehouse

[ERROR] Plugin org.springframework.boot:spring-boot-maven-plugin:2.3.0.RELEASE or one of its dependencies could not be resolved: Failed to read artifact descriptor
for org.springframework.boot:spring-boot-maven-plugin:jar:2.3.0.RELEASE: Could not transfer artifact org.springframework.boot:spring-boot-maven-plugin:pom:2.3.0.R
ELEASE from/to alimaven (http://maven.aliyun.com/nexus/content/repositories/central/): maven.aliyun.com: Unknown host maven.aliyun.com -> [Help 1]

Reference article:

It is said that both the local warehouse address and the mirror image address need to be pointed to the file address of the local warehouse
Say you need to turn on offline mode
The following settings are made

<localRepository>C:/Users/sliec/.m2/repository</localRepository>
.....
  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>true</offline>
  -->
  <offline>true</offline>

...
<mirror>
  <id>alimaven</id>
  <mirrorOf>central</mirrorOf>
  <name>aliyun maven</name>
  <url>file://C:/Users/sliec/.m2/repository </url>
</mirror>

After trying, I still can't.
At this time, the prompt says:

Cannot access nexus-aliyun in onffline mode and the artifact 
org.springframework.boot:spring-boot-starter-parent:pom:2.3.0.RELEASE 
has not been download from it before

Final solution:

When copying Maven related files, you need to copy the Maven package in the current networked environment, focusing on the configuration file, together with the local warehouse file at this moment.
The reason is that the Maven configuration file specifies the contents related to the mirror, and then there is an id of the mirror itself. Then, in each dependent package in the local warehouse, there is a file named:_ remote.repositories file, which records the following contents:

#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Fri May 29 15:05:16 CST 2020
spring-web-5.2.6.RELEASE-javadoc.jar>alimaven= //This place and setting Mirrorids in XML need to be consistent
spring-web-5.2.6.RELEASE.jar>alimaven=
spring-web-5.2.6.RELEASE.jar>central=
spring-web-5.2.6.RELEASE.pom>alimaven=
spring-web-5.2.6.RELEASE.pom>central=
spring-web-5.2.6.RELEASE-sources.jar>alimaven=
In other words, where did the dependency package come from? The above example shows that it came from the warehouse id For: alimaven,central Downloaded from the remote warehouse.
setting.xml Medium mirror Configuration content:
    <mirror>
        <id>alimaven</id>//Pay attention to this. It needs to be consistent with the dependency record file downloaded from the local warehouse_ remote.repositories consistent
        <mirrorOf>central</mirrorOf>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    </mirror>

If the warehouse id recorded in this list is the same as the Maven configuration file setting If the configured mirrorid in XML is inconsistent, Maven will think that the dependency stored in the local warehouse and the configured remote image address are not the same source when we rebuild through Maven in the LAN computer, so they cannot be used directly.
At this point, it will try according to setting The image address configured in XML to obtain a new dependency file. As a result, the LAN cannot access the image at this time, so an error will be reported, and you will be prompted that the image cannot be accessed, and the dependency has not been downloaded before. (I think this logic is also very strange. You can locate it in the library directly according to my artifact ID. what else do you want to do with the source)
After I copied the local warehouse and project into the office environment, I changed the setting because I didn't pay attention to it The mirrorid in the XML file and the local repository address lead to failure.

Tags: Maven

Posted by rodrigocaldeira on Wed, 11 May 2022 16:44:32 +0300