从 Eclipse 中的 Google Cloud Tools、Google App Engine Java 项目迁移到 Gradle

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

Google Cloud Tools for Eclipse 将于 2024 年 1 月 30 日过时,因为它缺乏对 Java 11+ 的支持,并且 Java 8 将无法部署。因此,我尝试从 Eclipse Standard Java App Engine 项目转换为使用 gradle 构建文件和 gcloud 应用程序部署。我在 java 项目的根目录中添加了一个 gradle.build 文件,如下所示:

buildscript {      // Configuration for building
  repositories {
    jcenter()      // Bintray's repository - a fast Maven Central mirror & more
    mavenCentral()
  } 

   dependencies {
    classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.5.0'
   }

}

repositories {   // repositories for Jar's you access in your code
  jcenter()
  mavenCentral()
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'com.google.cloud.tools.appengine'

dependencies {
  providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
  providedCompile 'com.google.appengine:appengine:+'
// Add your dependencies here.

}

  appengine {
    deploy {   // deploy configuration
      stopPreviousVersion = true  // default - stop the current version
      promote = true              // default - & make this the current version
    }
  }


sourceCompatibility = 11
targetCompatibility = 11

我直接从 Google 的示例中获取了此示例,并从中删除了 gretty 和 jetty 库,因为每当我尝试使用 gradle 构建时,我都会收到错误:无法为 org.gradle 类型的任务“:archiveProduct”设置未知属性“baseName”。 gradle.api.tasks.bundling.Zip。我认为这些库只是用于在本地运行应用程序,所以我认为删除它们没什么大不了的,但请让我知道是否有。

我还添加了一个 app.yaml 文件,如下所示:

runtime: java11
service: serviceName
entrypoint: java -cp "*" packagename.Driver

当我尝试部署代码时,gcloud app部署命令抱怨缺少主类,因此我在项目中添加了一个主类,尽管我认为它不需要它,因为没有它它也可以正常工作蚀。看起来像这样:

package packageName;

public class Driver {
    public static void main(String[] args) {
        System.out.println("This is a driver main class.");
    }
}

当我部署它时,我的应用程序引擎只是给出错误“服务不可用”,并且与 web.xml 映射的 servlet 都不起作用。

我不确定为什么 google 不只是在 Google Cloud Tools for Eclipse 中添加对 Java 11+ 的支持,但正因为如此,我必须处理这个问题。

如果有人知道如何从 Eclipse 获取标准 java appengine 项目并使用 gradle 进行部署,那就太好了。

java eclipse gradle google-app-engine google-cloud-tools
1个回答
0
投票

我想通了,你必须重新构建文件夹结构,如下所示:

your-app-engine-project/
│
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── YourApp.java
│   │   ├── appengine/
│   │   │   ── app.yaml
│   │   └── webapp/
│   │       ├── WEB-INF/
│   │       │   └── appengine-web.xml
│   │       └── index.jsp
├── build.gradle

我更改了 app.yaml 文件以具有以下入口点,而不是我的主类入口点: java -noverify -jar 'nameofwarfilefrombuild'.war

将 eclipse 项目中的所有 war 文件夹文件放入 webapp 中,并将所有 java 代码文件放在 src/main/ 下

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