我是使用Docker的新手。我在Gradle Build中有Google Sheet的“ QuickStart”教程Java程序的修改版(已修改为写入几个单元格而不是读取单元格,Sheet ID也已更改...原始的Quickstart链接在此处:https://developers.google.com/sheets/api/quickstart/java)。 Java程序可以独立运行,但是我打算编写一个Dockerfile,它将成功地从我目前拥有的Gradle Build中构建映像,因此可以在容器中运行。由于Dockerfile语法具有细微差别和通用性,并且因为Google Sheets Gradle Build具有Google依赖关系,所以我在弄清楚该怎么做时遇到了麻烦。请注意,原始应用程序使用“基本gradle构建”(您键入“ gradle init --type basic”进行构建),并且Java代码位于src / main / java目录中,同时包含必要的certificate.json文件(包含我的客户ID和密码,以便Google表格可以正常使用)位于src / main / resources中。我也在使用Gradle 5.2.1。 这是Java代码...
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;
import com.google.api.services.sheets.v4.model.UpdateValuesResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
public class SheetsQuickstart {
private static final String APPLICATION_NAME = "Google Sheets API Java Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
/**
* Creates an authorized Credential object.
* @param HTTP_TRANSPORT The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = SheetsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
//System.out.println("The getCredentials will be returned");
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
//Writes a few basic things to cells in a google sheet
public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
final String spreadsheetId = "16WoDySciZRhD3QSH2_V1bk5kf_IFCcrPe49NE8WRxVg";
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
ValueRange body = new ValueRange().setValues(getData());
UpdateValuesResponse result =
service.spreadsheets().values().update(spreadsheetId, "B1", body)
.setValueInputOption("RAW")
.execute();
}
public static List<List<Object>> getData() {
List<Object> data1 = new ArrayList<Object>();
data1.add("test1");
data1.add("test2");
data1.add("Row");
List<Object> data2 = new ArrayList<Object>();
data2.add("test3");
data2.add("test4");
data2.add("test5");
List<Object> data3 = new ArrayList<Object>();
data3.add("test6");
List<List<Object>> data = new ArrayList<List<Object>>();
data.add(data1);
data.add(data2);
data.add(data3);
return data;
}
}
我的build.gradle文件看起来像这样...
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'SheetsQuickstart'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
compile 'com.google.apis:google-api-services-sheets:v4-rev516-1.23.0'
}
同样,我使用了“ grovvy basic gradle build”,目录结构可以在开头附近的https://guides.gradle.org/creating-new-gradle-builds/处找到(也我在gradle目录的根目录中添加了src / main目录)。如果要“构建”此gradle构建,我这样做的最佳选择是什么?它仅需要一个dockerfile,是否有一个示例说明?我如何处理这个问题根本上有什么不对吗?谢谢。