java如何在Windows机器上用Maven创建Dockerfile,然后发送到远程Linux服务器以构建映像,然后运行?
我和Maven有一个Java项目建设。我想在Dockerfile生成时动态创建它。我的最终目标是创建一个可执行的Jar和Dockerfile,将它们发送到一个服务器,在那里Dockerfile用于构建将执行Jar的映像,然后运行容器。这将主要用于构建/测试过程,因此我不想在每次测试之前提交任何代码或将我的Jar发布到某种工件。我觉得对服务器进行4次单独的身份验证效率很低。我试图找到一个Maven插件,可以完成所有这些,但没有任何运气。有没有一个插件可以帮我完成所有这些,或者只是一个比我现在做的更好的方法?提前谢谢。如果答案有帮助,我会将其标记为有用,并将正确答案标记为有用。提前谢谢!下面是我目前在Maven插件方面的进展
<!--This plugin will Transfer the executable JAR and Dockerfile file to a remote server, build an image and run it -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!-- Create a Docerfile -->
<echo file="${project.build.directory}/Dockerfile.txt"
message="FROM java:8${line.separator}"
append="true"/>
<echo file="${project.build.directory}/Dockerfile.txt"
message="ADD ${pi.deployDirectory}/${project.build.finalName}-jar-with-dependencies.jar ${pi.deployDirectory}/demo/${project.build.finalName}-jar-with-dependencies.jar${line.separator}"
append="true"/>
<echo file="${project.build.directory}/Dockerfile.txt"
message="CMD ["java","-jar","${pi.deployDirectory}/demo/${project.build.finalName}-jar-with-dependencies.jar"]"
append="true"/>
<!-- ensure the target directory exists on the Server -->
<sshexec host="${server.host}" port="${server.port}" username="${server.user}" password="${server.password}"
trust="true" failonerror="false" verbose="true"
command="mkdir --parents ${server.deployDirectory}"/>
<!-- copy the JAR file to the Server -->
<scp
file="${project.build.directory}/${project.build.finalName}-jar-with-dependencies.jar"
todir="${server.user}:${server.password}@${server.host}:${server.deployDirectory}"
port="${server.port}" trust="true" verbose="true" failonerror="true">
</scp>
<!-- copy the Dockerfile file to the Server -->
<scp
file="${project.build.directory}/${project.build.finalName}-jar-with-dependencies.jar"
todir="${server.user}:${server.password}@${server.host}:${server.deployDirectory}"
port="${server.port}" trust="true" verbose="true" failonerror="true">
</scp>
<!-- TODO: Add section that will on the remote server build a container using the new Dockerfile and then run that container -->
<!-- run the JAR file on the Server no in container-->
<sshexec host="${server.host}" port="${server.port}" username="${server.user}"
password="${server.password}" trust="true" failonerror="false"
verbose="true"
command="java -jar ${server.deployDirectory}/${project.build.finalName}-jar-with-dependencies.jar"/>
</tasks>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
</plugin>
共 (0) 个答案