没有为此构建指定目标。您必须指定一个有效的生命周期阶段

Posted

技术标签:

【中文标题】没有为此构建指定目标。您必须指定一个有效的生命周期阶段【英文标题】:No goals have been specified for this build. You must specify a valid lifecycle phase 【发布时间】:2021-05-01 20:33:01 【问题描述】:

我在尝试通过 GitHub Actions 推送到 docker 时不断收到来自标题的错误。我有一个 Java Maven 应用程序。 当我手动构建和推送 docker 镜像时,一切正常,但它不能通过标题中指定的 GitHub Actions 自动运行。

这是我的工作流程操作:

 name: Docker Image CI

on:
  workflow_dispatch:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 8
      uses: actions/setup-java@v1
      with:
        java-version: 8
    - name: Setup the POM file
      run: mvn -f app-demo/pom.xml 
    - name: Build with Maven
      run: mvn clean install dependency:copy-dependencies
    - name: Login to GitHub Package Registry
      run: echo $ secrets.GITHUB_TOKEN  | docker login docker.pkg.github.com -u $ github.repository  --password-stdin
    - name: Build the Docker image
      run: docker build -t app-demo:latest .
    - name: Tag the Docker image
      run: docker tag app-demo:latest docker.pkg.github.com/repo/app-demo/app-demo:latest
    - name: Push the Docker image to the registry
      run: docker push docker.pkg.github.com/repo/app-demo/app-demo:latest

这是我的 POM 文件:

<?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.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.app.demo</groupId>
    <artifactId>app-demo</artifactId>
    <version>latest</version>
    <name>acs-simulator</name>
    <description>APP Demo</description>

    <properties>
        <java.version>8</java.version>
    </properties>




    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.2.1</version>
        </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-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <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>com.hivemq</groupId>
            <artifactId>hivemq-mqtt-client</artifactId>
            <version>1.2.1</version>
        </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>com.hivemq</groupId>
            <artifactId>hivemq-testcontainer-junit5</artifactId>
            <version>1.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.paho</groupId>
            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version> 3.2.2</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

我遇到了这个错误,我该如何解决?提前谢谢你。

【问题讨论】:

为什么在 Spring Boot 应用中使用 maven-shade-plugin?为什么要自己定义 JUnit Jupiter 版本?为什么要自己定义 mockito-all 版本?所有版本都由 spring boot 父级处理......我也建议将你的 spring boot 版本升级到最新版本(2.4.2)......Jackson 也是由 spring boot 定义的父... 【参考方案1】:

问题是这样的,我认为:

- name: Setup the POM file
  run: mvn -f app-demo/pom.xml 
- name: Build with Maven
  run: mvn clean install dependency:copy-dependencies

第一个run: 正在运行没有任何目标的maven。你需要一次运行......像这样:

- name: Build with Maven
  run: mvn -f app-demo/pom.xml clean install dependency:copy-dependencies

无论哪种方式,请检查 Maven documentation 以了解 mvn 命令行参数的含义。

【讨论】:

以上是关于没有为此构建指定目标。您必须指定一个有效的生命周期阶段的主要内容,如果未能解决你的问题,请参考以下文章

spring中bean生命周期

具有自定义生命周期的身份服务器 4 访问令牌

Maven 构建生命周期

Maven 构建生命周期

不安全指定生命周期的例子都有哪些? [复制]

指定 Rust 闭包的生命周期