尝试对 Maven 项目进行 docker 化

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

我正在尝试对 Apache Commons Daemons 项目进行 docker 化。目录是这样的: 项目目录

我已经遵循了这个指南:https://www.educative.io/answers/how-do-you-dockerize-a-maven-project并且在mvn安装之前我没有任何问题。

然后,当我尝试进行 docker 构建时,出现以下错误:

------
 > [2/2] ADD target/simple-docker.jar simple-docker.jar:
------
Dockerfile:21
--------------------
  19 |     
  20 |     FROM openjdk:8
  21 | >>> ADD target/simple-docker.jar simple-docker.jar
  22 |     ENTRYPOINT ["java", "-jar","simple-docker.jar"]
  23 |     EXPOSE 8080
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 98866f48-ea55-426c-80eb-d2380731e691::i1dew1l2luwxdy8xrwia5eytv: "/target/simple-docker.jar": not found

这是 Dockerfile:

 # or more contributor license agreements.  See the NOTICE file
 # distributed with this work for additional information
 # regarding copyright ownership.  The ASF licenses this file
 # to you under the Apache License, Version 2.0 (the
 # "License"); you may not use this file except in compliance
 # with the License.  You may obtain a copy of the License at
 #
 #   http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing,
 # software distributed under the License is distributed on an
 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
 #

FROM openjdk:8
ADD target/simple-docker.jar simple-docker.jar
ENTRYPOINT ["java", "-jar","simple-docker.jar"]
EXPOSE 8080

这是pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <parent>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-parent</artifactId>
    <version>77</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>commons-daemon</groupId>
  <artifactId>commons-daemon</artifactId>
  <version>1.4.1-SNAPSHOT</version>
  <name>Apache Commons Daemon</name>
  <!-- Daemon started in Commons in 2002 with an import of code from Tomcat,
       which is why the NOTICE file has an earlier date than the inceptionYear -->
  <inceptionYear>2002</inceptionYear>
  <description>
    Apache Commons Daemon software is a set of utilities and Java support
    classes for running Java applications as server processes. These are
    commonly known as 'daemon' processes in Unix terminology (hence the
    name). On Windows they are called 'services'.
  </description>
  <url>https://commons.apache.org/proper/commons-daemon/</url>

  <issueManagement>
    <system>jira</system>
    <url>https://issues.apache.org/jira/browse/DAEMON</url>
  </issueManagement>

  <scm>
    <connection>scm:git:https://gitbox.apache.org/repos/asf/commons-daemon.git</connection>
    <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/commons-daemon.git</developerConnection>
    <url>https://gitbox.apache.org/repos/asf?p=commons-daemon.git</url>
  </scm>

  <developers>
    <developer>
      <name>Jean-Frederic Clere</name>
      <id>jfclere</id>
      <email>jfclere at apache.org</email>
    </developer>

    <developer>
      <name>Remy Maucherat</name>
      <id>remm</id>
      <email>remm at apache.org</email>
    </developer>

    <developer>
      <name>Yoav Shapira</name>
      <id>yoavs</id>
      <email>yoavs at apache.org</email>
    </developer>

    <developer>
      <name>Bill Barker</name>
      <id>billbarker</id>
      <email>billbarker at apache.org</email>
    </developer>
    <developer>
      <name>Mladen Turk</name>
      <id>mturk</id>
      <email>mturk at apache.org</email>
    </developer>

    <developer>
      <name>Pier Fumagalli</name>
      <id>pier</id>
      <email>pier at apache.org</email>
    </developer>

    <developer>
      <id>ggregory</id>
      <name>Gary Gregory</name>
      <email>ggregory at apache.org</email>
      <url>https://www.garygregory.com</url>
      <organization>The Apache Software Foundation</organization>
      <organizationUrl>https://www.apache.org/</organizationUrl>      
      <roles>
        <role>PMC Member</role>
      </roles>
      <timezone>America/New_York</timezone>
      <properties>
        <picUrl>https://people.apache.org/~ggregory/img/garydgregory80.png</picUrl>
      </properties>
    </developer>

  </developers>

  <contributors>
    <contributor>
      <name>Nick Griffiths</name>
      <email>[email protected]</email>
    </contributor>
  </contributors>

  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <properties>
    <!-- Apache Commons Daemon is used by Apache Tomcat. The oldest currently
         supported Tomcat version is Tomcat 9 which has a minimum Java version
         of Java 8. It is therefore advisable to check with the Apache Tomcat
         community before increasing these values. -->
    <animal-sniffer.signature>java18</animal-sniffer.signature>
    <commons.daemon.javaversion>1.8</commons.daemon.javaversion>
    <maven.compiler.source>${commons.daemon.javaversion}</maven.compiler.source>
    <maven.compiler.target>${commons.daemon.javaversion}</maven.compiler.target>
    <commons.componentid>daemon</commons.componentid>
    <commons.module.name>org.apache.commons.daemon</commons.module.name>
    <commons.release.version>1.4.1</commons.release.version>
    <commons.rc.version>RC1</commons.rc.version>
    <commons.bc.version>1.4.0</commons.bc.version>
    <commons.jira.id>DAEMON</commons.jira.id>
    <commons.jira.pid>12310468</commons.jira.pid>
    <project.build.outputTimestamp>2024-01-01T00:00:00Z</project.build.outputTimestamp>
    <commons.jacoco.haltOnFailure>true</commons.jacoco.haltOnFailure>
    <!-- JaCoCo: Don't make code coverage worse than: -->
    <commons.jacoco.haltOnFailure>true</commons.jacoco.haltOnFailure>
    <commons.jacoco.classRatio>0.01</commons.jacoco.classRatio> <!-- abbassato a 0.01 da 0.12 -->
    <commons.jacoco.instructionRatio>0.00</commons.jacoco.instructionRatio>
    <commons.jacoco.methodRatio>0.01</commons.jacoco.methodRatio>
    <commons.jacoco.branchRatio>0.00</commons.jacoco.branchRatio>
    <commons.jacoco.lineRatio>0.00</commons.jacoco.lineRatio>
    <commons.jacoco.complexityRatio>0.00</commons.jacoco.complexityRatio>
  </properties>
  <profiles>
    <!-- Customise the default ASF Commons release profile -->
    <profile>
      <id>release</id>
      <build>
        <finalName>simple-docker</finalName>
        <plugins>
          <!--
            Disable the default detach-distributions goal.
            Project specific configuration is provided below.
          -->
          <plugin>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-release-plugin</artifactId>
            <executions>
              <execution>
                <!-- Spelling error is deliberate to match parent pom -->
                <id>detatch-distributions</id>
                <phase>none</phase>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  
  <build>
    <defaultGoal>clean verify apache-rat:check japicmp:cmp pmd:check pmd:cpd-check javadoc:javadoc</defaultGoal>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.rat</groupId>
          <artifactId>apache-rat-plugin</artifactId>
          <configuration>
            <excludes>
              <!-- Free Software (see header) -->
              <exclude>src/native/unix/configure</exclude>
              <!-- GPL with applicable exception that allows ALv2 -->
              <exclude>src/native/unix/support/config.guess</exclude>
              <exclude>src/native/unix/support/config.sub</exclude>
              <!-- Files created by Configure -->
              <!-- Excluded from Git but may be present locally -->
              <exclude>src/native/unix/config.nice</exclude>
              <exclude>src/native/unix/config.status</exclude>
              <exclude>src/native/unix/config.log</exclude>
            </excludes>
          </configuration>
        </plugin>
        <plugin>
          <groupId>com.github.spotbugs</groupId>
          <artifactId>spotbugs-maven-plugin</artifactId>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptors>
            <descriptor>src/assembly/native-src.xml</descriptor>
            <descriptor>src/assembly/bin.xml</descriptor>
            <descriptor>src/assembly/src.xml</descriptor>
            <descriptor>src/assembly/win.xml</descriptor>
          </descriptors>
          <tarLongFileMode>gnu</tarLongFileMode>
        </configuration>
      </plugin>
      <plugin>
        <!-- Build an executable JAR -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.4.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>org.apache.commons.daemon.Docker.example</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>

      <!-- Force detach-distributions to execute *after* Nexus deploy -->
      <!-- we want these distributions deployed to both Nexus and dist.a.o -->
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-release-plugin</artifactId>
        <executions>
          <execution>
            <id>detach-distributions</id>
            <phase>deploy</phase>
            <goals>
              <goal>detach-distributions</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

还有一些额外信息:

  • 类路径
  • mvn install 中唯一的警告: [警告] 文件 commons-daemon-1.4.1-SNAPSHOT.jar 已经是模块化的
  • 示例类代码:
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.commons.daemon.Docker;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class example
{
    public static void main( String[] args ) throws IOException {

        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
        server.createContext("/", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();

    }

    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {
            String response = "<h1> Hello World!!!! I just Dockerized a Maven Project </h1>";
            t.sendResponseHeaders(200, response.length());
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }
}

如果需要任何其他信息,我会尽快提供。

谢谢你

java docker apache maven docker-compose
1个回答
0
投票

mvn 原型:生成 -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

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