如何将Maven注入到我的docker镜像中?

问题描述 投票:0回答:1

我有一个docker镜像,必须要有 程序上 编译代码作为它的运行时算法的一部分。它使用了以下代码。

 if (!Files.exists(rootPath)) {
            throw new BoilerplateBuildException(String.format("The directory to compile \"%s\" does not exist.", rootPath == null ? "N/A" : rootPath.toString()));
        }

        Path pomPath = Paths.get(rootPath.toString(), "pom.xml");
        if (!Files.exists(pomPath)) {
            throw new BoilerplateBuildException(String.format("The MAVEN pom file \"%s\" does not exist.", pomPath.toString()));
        }

        InvocationRequest request = new DefaultInvocationRequest();
        request.setPomFile( new File( pomPath.toString() ) );
        request.setGoals(Arrays.asList("compile"));
        request.setBaseDirectory(new File(rootPath.toString()));

        Invoker invoker = new DefaultInvoker();
        try {
            invoker.setMavenHome(new File(System.getenv("MAVEN_HOME")));

            InvocationResult result = invoker.execute( request );

            if(result != null && result.getExitCode() != 0){
                throw new BoilerplateBuildException(String.format("Failed to build with maven in path \"%s\" (Check the inner exception for further details).", pomPath.toString()), result.getExecutionException());
            }
        } catch (MavenInvocationException e) {
            throw new RuntimeException(String.format("Failed to build with maven for \"%s\"", pomPath.toString()), e);
        }

使用以下工件

 <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-invoker</artifactId>
            <version>3.0.1</version>
 </dependency>

但这只是一个客户。而不是Maven本身。当然,在本地开发机器上是可以工作的,因为我已经安装了Maven,但当我的代码以docker容器的形式运行时,就完全不同了。但当我的代码以docker容器的形式运行时,情况就完全不同了。. 所以我的问题是--我可以把Maven "注入 "到我的docker镜像中吗(当然是用它的环境变量,如 MAVEN_HOME),这样它就可以用于我的代码了?

docker maven
1个回答
1
投票

请使用 maven 镜像中已经安装了java。也许,你可以选择以下标签 mavenjava 你想使用的版本。

参考:- https:/hub.docker.com_maven。

使用maven的基础图像,你可以从以下方面构建你的图像 Dockerfile.

参考文献:- https:/docs.docker.comenginereferencebuilder。

并在容器内,你将能够执行 mavenjava 命令

© www.soinside.com 2019 - 2024. All rights reserved.