Maven offline - problem with mvn-plugins

前端 未结 6 1883
滥情空心
滥情空心 2021-01-12 06:49

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, bu

相关标签:
6条回答
  • 2021-01-12 07:18

    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>
    
    0 讨论(0)
  • 2021-01-12 07:21

    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
    
    0 讨论(0)
  • 2021-01-12 07:24

    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

    0 讨论(0)
  • 2021-01-12 07:33

    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.

    0 讨论(0)
  • 2021-01-12 07:34

    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

    0 讨论(0)
  • 2021-01-12 07:37

    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
    

    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
    

    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
    

    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
    

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

    FROM JRE
    COPY --from package /usr/src/app/target /bin
    ...
    ...
    
    0 讨论(0)
提交回复
热议问题