rafa.ferreira
rafa.ferreira

Reputation: 2017

Maven offline - problem with mvn-plugins

I'm using maven in my project and I need to run the build in a non-internet-access machine.

When I test my project build everything is working, but when I run the build in a future moment, the maven try to update the mvn-plugins and this sht* is broking my build.

My config file: settings.xml from mvn.

    <profile>
      <id>blaProfile</id>
      <repositories>
        <repository>
          <id>blaRepo</id>
          <url>file://${bla.3rdParty.home}/maven/.m2/repository</url>
          <layout>default</layout>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>blaRepo</id>
          <url>file://${bla.3rdParty.home}/maven/.m2/repository</url>
          <layout>default</layout>
        </pluginRepository>
      </pluginRepositories>
    </profile>

  <activeProfiles>
    <activeProfile>blaProfile</activeProfile>
  </activeProfiles>

And I ran my maven is with the params:

mvn -npu -bla.3rdParty.home="$(THE_CORRECT_PATH)" package

I saw that maven try to update some mvn-plugins for some time, but the option:

-npu,--no-plugin-updates      Suppress upToDate check for any relevant

Should work for this updates.

Well waiting some help on that!

Thanks in advance!


UPDATE(1):
What I'm looking at, is that I could use the setting:

<usePluginRegistry>true</usePluginRegistry>

Inside my settings.xml and with this, I'll have a plugin-registry.xml inside ${user.home}/.m2 that I can config and force the maven plugins versions.

But it's not working! :(

Upvotes: 12

Views: 20892

Answers (6)

Marcello DeSales
Marcello DeSales

Reputation: 22319

Maven Go-offline + Isolated Docker Multi-stage Image Builds

My answer is for both a local build or a Dockerized environment, which is isolated on the nature of how docker images are built. This uses Maven 3.6.3-jdk-8.

With this answer, you know exactly how much time your CI spends on downloading, compiling, testing, packaging...

Finally, also answering to the old question on Jira for the go-offline https://issues.apache.org/jira/browse/MDEP-82?focusedCommentId=16997793&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-16997793

  • Update pom.xml dependencies
    • maven-dependency-plugin
    • surefire-junit-platform
  • Call go-offline resolving dependencies
  • Call any mvn command with the switch --off-line

Set latest versions of the plugins

@@ -23,6 +23,9 @@
         <junit-jupiter.version>5.5.2</junit-jupiter.version>
         <common-io.version>2.6</common-io.version>
         <jacoco-maven-plugin.version>0.8.4</jacoco-maven-plugin.version>
+        <!-- https://issues.apache.org/jira/browse/MDEP-82 -->
+        <maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
+        <surefire-junit-platform.version>2.22.2</surefire-junit-platform.version>
         <maven-release-plugin.version>2.5.3</maven-release-plugin.version>
         <maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
         <maven-surefire-report-plugin.version>2.22.2</maven-surefire-report-plugin.version>
...
...
     <build>
         <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <version>${maven-dependency-plugin.version}</version>
+            </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
@@ -135,6 +143,11 @@
                     <target>${java.version}</target>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.surefire</groupId>
+                <artifactId>surefire-junit-platform</artifactId>
+                <version>${surefire-junit-platform.version}</version>
+            </plugin>

Create the Go-offline cache

  • This will ensure all the dependencies are downloaded
  • More than 3m to download all dependencies
FROM maven:3.6.3-jdk-8 AS dependencies-downloaded
...
...
COPY pom.xml /usr/src/app/pom.xml
COPY settings.xml /usr/src/app/settings.xml
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml dependency:resolve-plugins dependency:go-offline

enter image description here

Call compile with --offline

  • We can reuse the same image for compilation
  • Only takes 7s because nothing is downloaded
FROM dependencies-downloaded AS compile
COPY app /usr/src/app
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml compile --offline

enter image description here

Call tests with --offline

  • We can reuse the same image for tests
  • Taking 18s to run the test cases, without any download whatsoever
FROM compile AS tests
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml test --offline

enter image description here

Call package with --offline

  • We can reuse the same image for the final jar
  • Skipping even the tests ran in the previous docker image
  • Taking way less than before
FROM tests AS package
WORKDIR /usr/src/app
RUN mvn -f pom.xml -s settings.xml package -Dmaven.test.skip=true --offline

enter image description here

The final runtime image is a Docker image from the package.

FROM JRE
COPY --from package /usr/src/app/target /bin
...
...

Upvotes: 6

bedla.czech
bedla.czech

Reputation: 1219

After some debugging I found that maven-dependency-plugin (version 3.1.1 at the time of writing) is unable to resolve plugin's dependencies when specified like:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>3.0.0-M3</version>
 <dependencies>
  <dependency>    <--- this is not going to be resolved by dependency:go-offline command !!!
   <groupId>org.apache.maven.surefire</groupId>
   <artifactId>surefire-junit4</artifactId>
   <version>3.0.0-M3</version>
  </dependency>
 </dependencies>
</plugin>

After that I found go-offline-maven-plugin which just works! Pls see https://github.com/qaware/go-offline-maven-plugin for more info.

<plugin>
 <groupId>de.qaware.maven</groupId>
 <artifactId>go-offline-maven-plugin</artifactId>
 <version>x.y.z</version>
</plugin>

Current version could be found here https://mvnrepository.com/artifact/de.qaware.maven/go-offline-maven-plugin and Maven issue here https://issues.apache.org/jira/browse/MDEP-82

Upvotes: 1

evgeni
evgeni

Reputation: 1755

It should suffice to run a mvn clean install on your code. The next time, when you run it in an offline mode you can use the following options:

mvn -Dmaven.repo.local=..\repository –o clean install

-o tells Maven not to try to update its dependencies over the network and with -Dmaven.repo.local you provide the path of the repository which contains all the required dependencies. Alternatively, you can add the path of the repository in your settings.xml in the localRepository tag and add an <offline>true</offline>. You can configure the same in your Maven Eclipse configurations.

Upvotes: 4

itaifrenkel
itaifrenkel

Reputation: 1598

In order to cache plugins into the .m2/repository folder you would need to specify all plugins explicitly with the mvn <maven-plugin-name>:help

You would also need to specify explicit version for each plugin in the <plugins> or <pluginsManagement> section of your pom.xml

<plugin> 
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19</version>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-testng</artifactId>
      <version>2.19</version>
    </dependency>
  </dependencies>
</plugin>

This is needed to make sure that mvn install -o uses the same plugin version.

Ofcourse you would also need to run mvn dependency:go-offline to take care of your compile and test dependencies.

mvn assembly:help compiler:help enforcer:help exec:help failsafe:help install:help jar:help resources:help surefire:help mvn dependency:go-offline mvn compile --offline

Upvotes: 6

Rich Seller
Rich Seller

Reputation: 84038

I think this happens because Maven hasn't got the metadata available locally to determine if its plugin versions are correct. If you specify exact versions for your plugins (which is a good idea for reproducability anyway), it doesn't need to do the check, so will not try to connect to the remote repositories.

By specify exact versions, I mean that in your project's POM you should add the version to the plugin declaration. For example:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <!-- set the version explicitly-->
    <version>2.0</version>
  </plugin>
</plugins>

Note you can also force Maven to use internal repositories instead of Central by setting up repository mirrors. See this answer for more details on using repository managers and mirrors.

In the config you included, you're setting your remote repository to point to your local repository, this is not a good idea. To run offline you should either pass -o at the command line or add this to your settings.xml:

<offline>true</offline>

Upvotes: 0

hohonuuli
hohonuuli

Reputation: 2014

Before you go offline run the following:

mvn dependency:go-offline

That will download all your dependencies and plugins that you need to build your project into ~/.m2/repository.

Once you've run that you can now build your project offline using the '-o' flag:

mvn install -o

Upvotes: 10

Related Questions