Dockerfile无法使用Maven构建应用-此构建未指定目标 [英] Dockerfile can't build app with Maven - No goals specified for this build

查看:55
本文介绍了Dockerfile无法使用Maven构建应用-此构建未指定目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有多个数据服务的Spring Boot应用程序,但我正努力使其作为容器化应用程序工作.这是项目文件夹结构:

I'm developing a Spring Boot app with several data services, but I'm struggling to get it working as a containerized application. This is the project folder structure:

myapp/
├── src/
│   └── ...
├── docker-compose.yml
├── Dockerfile
├── init-mongo.js
└── pom.xml

这是我在阅读教程和官方文档后想到的Dockerfile内容:

And this is the Dockerfile content I came up with after reading tutorials and official documentation:

#
# Build stage
#
FROM maven:3.5-jdk-8 AS build
COPY init-mongo.js /docker-entrypoint-initdb.d/
COPY src /app/src/
COPY pom.xml /app/
RUN mvn -f /app/pom.xml clean package

#
# Package stage
#
FROM openjdk:8-alpine
COPY --from=build /app/target/*.jar /usr/app/app.jar
ENTRYPOINT ["java","-jar","/usr/app/app.jar"]

这是我的docker-compose.yml:

And this is my docker-compose.yml:

version: '3'
services:
  app-mongodb:
    container_name: app-mongodb
    image: mongo
    environment:
      MONGO_INITDB_DATABASE: appdb
      MONGO_INITDB_ROOT_USERNAME: mongodbusr
      MONGO_INITDB_ROOT_PASSWORD: mongodbpwd
    volumes:
      - mongodata:/usr/share/mongodb/data
    ports:
      - "27017:27017"
    hostname: app-mongodb

  app-mariadb:
    container_name: app-mariadb
    image: mariadb
    environment:
      MYSQL_DATABASE: appdb
      MYSQL_ROOT_PASSWORD: mariadbpwd
      MYSQL_ROOT_HOST: '%'
    ports:
      - "3306:3306"
    restart: always

  app-elasticsearch:
    container_name: app-elasticsearch
    image: docker.elastic.co/elasticsearch/elasticsearch:6.8.6
    volumes:
      - esdata:/usr/share/elasticsearch/data
    ports:
      - "9200:9200"
    hostname: app-elasticsearch

  app:
    build: .
    ports:
      - 8080:8080
    depends_on:
      - app-mariadb
      - app-mongodb
      - app-elasticsearch

volumes:
  mongodata:
  esdata:

这是应该的,但我每次运行 docker-compose up 命令时,都会出现Maven错误:

Thing is it should work, but I keep getting Maven error whenever I run the docker-compose up command:

myapp_1            | [INFO] Scanning for projects...
myapp_1            | [INFO] ------------------------------------------------------------------------
myapp_1            | [INFO] BUILD FAILURE
myapp_1            | [INFO] ------------------------------------------------------------------------
myapp_1            | [INFO] Total time: 0.216 s
myapp_1            | [INFO] Finished at: 2020-10-24T19:25:15Z
myapp_1            | [INFO] ------------------------------------------------------------------------
myapp_1            | [ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal>
 or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate
-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, pr
ocess-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clea
n, pre-site, site, post-site, site-deploy. -> [Help 1]
myapp_1            | [ERROR]
myapp_1            | [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
myapp_1            | [ERROR] Re-run Maven using the -X switch to enable full debug logging.
myapp_1            | [ERROR]
myapp_1            | [ERROR] For more information about the errors and possible solutions, please read the following articles:
myapp_1            | [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException

我想念什么?

编辑:我尝试显式打包JAR文件,然后按照@BeppeC的建议,将其复制到编辑Dockerfile内容的最终容器中:

EDIT: I tried explicitly packaging the JAR file and then just copying it to the end container editing the Dockerfile content, as suggested by @BeppeC:

FROM openjdk:8-alpine
COPY target/*.jar /usr/app/app.jar
ENTRYPOINT ["java","-jar","/usr/app/app.jar"]

它仍然产生相同的错误,因此似乎有一些触发我不知道的mvn构建的事件.

It's still producing the same error, so it seems there's something triggering the mvn build which I'm not aware of.

EDIT2 :我怀疑是什么原因导致了无目标"?错误可能是我的pom.xml文件有问题:

EDIT2: I suspect what is causing the "no goals" error might be something wrong with my pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>it.clsoft</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>myapp</name>
    <description>My web app</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.2.10.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.bitbucket.b_c</groupId>
            <artifactId>jose4j</artifactId>
            <version>0.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.19</version>
        </dependency>

        <dependency>
            <groupId>org.tukaani</groupId>
            <artifactId>xz</artifactId>
            <version>1.8</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

        <dependency>
            <groupId>com.github.sisyphsu</groupId>
            <artifactId>dateparser</artifactId>
            <version>1.0.4</version>
        </dependency>

        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>com.github.darrachequesne</groupId>
            <artifactId>spring-data-jpa-datatables</artifactId>
            <version>5.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.github.darrachequesne</groupId>
            <artifactId>spring-data-mongodb-datatables</artifactId>
            <version>1.0.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.4.4</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.12.4</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.owasp.esapi</groupId>
            <artifactId>esapi</artifactId>
            <version>2.2.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

这对我来说似乎不错,但是我还是按照教程的方式工作,所以我可能错过了一些重要的东西.

It seems alright to me, but again I'm working my way through tutorials so I might've missed something important.

推荐答案

如果我更改以下内容,则对我有用:

Works for me if I change from:

#
# Build stage
#
FROM maven:3.5-jdk-8 AS build
COPY init-mongo.js /docker-entrypoint-initdb.d/
COPY src /app/src/
COPY pom.xml /app/
RUN mvn -f /app/pom.xml clean package

收件人:

#
# Build stage
#
FROM maven:3.5-jdk-8 AS build
COPY init-mongo.js /docker-entrypoint-initdb.d/
COPY src /app/src/
COPY pom.xml /app/
WORKDIR /app
RUN mvn clean package

我看到的主要问题纯粹是当前工作目录中没有pom../(根目录)..将 WORKDIR 设置为/app 效果很好.

Main problem I saw was purely that no pom was available in the current working dir.. / (root dir).. setting the WORKDIR to the /app worked perfectly though.

这篇关于Dockerfile无法使用Maven构建应用-此构建未指定目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆