У меня есть изображение 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, это совсем другая история .
Итак, мой вопрос - могу ли я «вставить» Maven в мое docker изображение (с его переменными среды конечно вроде MAVEN_HOME
) так он будет доступен моему коду?