如何在Windows机器上使用Maven创建Dockerfile,然后发送到远程Linux服务器以构建映像然后运行?

时间:2016-02-08 16:40:40

标签: java maven ant docker dockerfile

我有一个使用Maven构建的Java项目。我想在构建时动态创建Dockerfile。我的最终目标是创建一个可执行的Jar和Dockerfile,将它们发送到服务器,其中Dockerfile用于构建将执行Jar的映像,然后运行容器。这将主要用于构建/测试过程,因此我不想在每次测试之前提交任何代码或将我的Jar发布到排序的Artifactory。我觉得对服务器的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 [&quot;java&quot;,&quot;-jar&quot;,&quot;${pi.deployDirectory}/demo/${project.build.finalName}-jar-with-dependencies.jar&quot;]"
                                  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 个答案:

没有答案