Skip to content

Commit

Permalink
Add a new goal to generate sources locally
Browse files Browse the repository at this point in the history
- add the goal clientSrc to generate client
  API code locally. Requires the installation
  of the code generator (python module)
- fix warnings in unit tests
- clean config files: README, poms
  • Loading branch information
Loïc Lambert committed Jan 29, 2020
1 parent ba97513 commit 8d047e3
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.idea
target
*.iml
.classpath
.project
.settings/
.checkstyle
log.txt
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: xenial
language: java
jdk:
- openjdk8
Expand All @@ -6,7 +7,10 @@ cache:
directories:
- $HOME/.m2/repository

before_install: cp .travis/settings.xml $HOME/.m2/settings.xml
before_install:
- cp .travis/settings.xml $HOME/.m2/settings.xml
- sudo apt-get -y install python2.7 python-pip
- python2 -m pip install --user google-apis-client-generator

deploy:
provider: script
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
![project status image](https://img.shields.io/badge/stability-stable-brightgreen.svg)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.google.cloud.tools/endpoints-framework-maven-plugin/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.google.cloud.tools/endpoints-framework-maven-plugin)
[![Build Status](https://api.travis-ci.org/AODocs/endpoints-framework-maven-plugin.svg?branch=master)](https://travis-ci.org/AODocs/endpoints-framework-maven-plugin)
# Endpoints Framework Maven plugin

This Maven plugin provides goals and configurations to build Endpoints Framework projects.
Expand Down Expand Up @@ -27,12 +26,14 @@ All goals are prefixed with `endpoints-framework`

The plugin exposes the following server side goals
* `clientLibs` - generate client libraries
* `clientSrc` - generate client code locally. Requires `python 2.7` and the python package `google-apis-client-generator`
* `discoveryDocs` - generate discovery docs
* `openApiDocs` - generate Open API docs

The plugin exposes the following parameters for configuring server side goals
* `discoveryDocDir` - The output directory of discovery documents
* `clientLibDir` - The output directory of client libraries
* `generatedSrcDir` - The output directory of generated endpoints source
* `openApiDocDir` - The output directory of Open API documents
* `serviceClasses` - List of service classes (optional), this can be inferred from web.xml
* `webappDir` - Location of webapp directory
Expand Down
22 changes: 8 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<groupId>com.google.cloud.tools</groupId>
<artifactId>endpoints-framework-maven-plugin</artifactId>
<!--Version must be in sync with the endpoints framework version-->
<version>2.4.1-aodocs</version>
<version>2.4.2-aodocs-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Endpoints Framework Maven Plugin</name>
<description>This Maven plugin provides goals to generate Google Cloud Endpoints Framework client code and discovery docs</description>
<url>https://github.com/GoogleCloudPlatform/endpoints-framework-maven-plugin</url>
<url>https://github.com/AODocs/endpoints-framework-maven-plugin</url>

<licenses>
<license>
Expand All @@ -19,33 +19,27 @@
</license>
</licenses>

<developers>
<developer>
<id>loosebazooka</id>
<name>Appu Goundan</name>
<email>[email protected]</email>
</developer>
</developers>

<scm>
<connection>scm:git:https://github.com/GoogleCloudPlatform/endpoints-framework-maven-plugin.git
<connection>scm:git:https://github.com/AODocs/endpoints-framework-maven-plugin.git
</connection>
<developerConnection>scm:git:https://github.com/GoogleCloudPlatform/endpoints-framework-maven-plugin.git
<developerConnection>scm:git:https://github.com/AODocs/endpoints-framework-maven-plugin.git
</developerConnection>
<url>https://github.com/GoogleCloudPlatform/endpoints-framework-maven-plugin</url>
<url>https://github.com/AODocs/endpoints-framework-maven-plugin</url>
</scm>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Should update this version in src/test/resources/projects/server/pom.xml -->
<endpoints-framework.version>2.4.2</endpoints-framework.version>
</properties>

<dependencies>
<dependency>
<groupId>com.aodocs.endpoints</groupId>
<artifactId>endpoints-framework-tools</artifactId>
<version>2.4.1</version>
<version>${endpoints-framework.version}</version>
</dependency>

<!--Maven API-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public abstract class AbstractEndpointsWebAppMojo extends AbstractMojo {

@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject project;
MavenProject project;

/** Location of compile java classes. */
@Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true)
Expand All @@ -61,6 +61,7 @@ public abstract class AbstractEndpointsWebAppMojo extends AbstractMojo {

@Override
public final void execute() throws MojoExecutionException {
preExecute();
File outputDirectory = getOutputDirectory();
if (!outputDirectory.exists() && !outputDirectory.mkdirs()) {
throw new MojoExecutionException(
Expand Down Expand Up @@ -109,6 +110,8 @@ private List<String> createParameterList(String actionName)
return params;
}

protected void preExecute() throws MojoExecutionException {}

protected abstract String getActionName();

protected abstract File getOutputDirectory();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2016 Google Inc.
*
* Licensed 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 com.google.cloud.tools.maven.endpoints.framework;

import com.google.api.server.spi.tools.GetClientSrcAction;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

/** Maven goal to create generated source dir from endpoints. */
@Mojo(
name = "clientSrc",
requiresDependencyResolution = ResolutionScope.COMPILE,
defaultPhase = LifecyclePhase.GENERATE_SOURCES)
public class ClientSrcMojo extends AbstractEndpointsWebAppMojo {

/** Output directory for generated sources. */
@Parameter(
defaultValue = "${project.build.directory}/generated-sources/endpoints",
property = "endpoints.generatedSrcDir",
required = true)
private File generatedSrcDir;

@Override
protected void preExecute() throws MojoExecutionException {
if (!generatedSrcDir.exists() && !generatedSrcDir.mkdirs()) {
throw new MojoExecutionException(
"Failed to create output directory: " + generatedSrcDir.getAbsolutePath());
}
project.addCompileSourceRoot(generatedSrcDir.getAbsolutePath());
}

@Override
protected String getActionName() {
return GetClientSrcAction.NAME;
}

@Override
protected File getOutputDirectory() {
return generatedSrcDir;
}

@Override
protected String getAdditionalParameters() {
return null;
}

@Override
protected void addSpecificParameters(List<String> params) {
params.addAll(Arrays.asList("-l", "java"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;
import org.junit.rules.TemporaryFolder;

public class ClientLibsMojoTest {
Expand Down Expand Up @@ -63,7 +62,7 @@ public void testDefault() throws IOException, VerificationException, XmlPullPars

String apiJavaFile =
getFileContentsInZip(new File(testDir, CLIENT_LIB_PATH), API_JAVA_FILE_PATH);
Assert.assertThat(apiJavaFile, JUnitMatchers.containsString(DEFAULT_URL_VARIABLE));
Assert.assertThat(apiJavaFile, CoreMatchers.containsString(DEFAULT_URL_VARIABLE));
}

@Test
Expand All @@ -76,10 +75,10 @@ public void testApplicationId()
String apiJavaFile =
getFileContentsInZip(new File(testDir, CLIENT_LIB_PATH), API_JAVA_FILE_PATH);
Assert.assertThat(
apiJavaFile, CoreMatchers.not(JUnitMatchers.containsString(DEFAULT_URL_VARIABLE)));
apiJavaFile, CoreMatchers.not(CoreMatchers.containsString(DEFAULT_URL_VARIABLE)));
Assert.assertThat(
apiJavaFile,
JUnitMatchers.containsString(
CoreMatchers.containsString(
DEFAULT_URL_PREFIX + "\"https://maven-test.appspot.com/_ah/api/\";"));
}

Expand All @@ -93,10 +92,10 @@ public void testHostname() throws IOException, VerificationException, XmlPullPar
String apiJavaFile =
getFileContentsInZip(new File(testDir, CLIENT_LIB_PATH), API_JAVA_FILE_PATH);
Assert.assertThat(
apiJavaFile, CoreMatchers.not(JUnitMatchers.containsString(DEFAULT_URL_VARIABLE)));
apiJavaFile, CoreMatchers.not(CoreMatchers.containsString(DEFAULT_URL_VARIABLE)));
Assert.assertThat(
apiJavaFile,
JUnitMatchers.containsString(DEFAULT_URL_PREFIX + "\"https://my.hostname.com/_ah/api/\";"));
CoreMatchers.containsString(DEFAULT_URL_PREFIX + "\"https://my.hostname.com/_ah/api/\";"));
}

@Test
Expand All @@ -110,16 +109,17 @@ public void testBasePath() throws IOException, VerificationException, XmlPullPar
String apiJavaFile =
getFileContentsInZip(new File(testDir, CLIENT_LIB_PATH), API_JAVA_FILE_PATH);
Assert.assertThat(
apiJavaFile, CoreMatchers.not(JUnitMatchers.containsString(DEFAULT_BASE_PATH)));
apiJavaFile, CoreMatchers.not(CoreMatchers.containsString(DEFAULT_BASE_PATH)));
Assert.assertThat(
apiJavaFile,
JUnitMatchers.containsString(
CoreMatchers.containsString(
DEFAULT_URL_PREFIX + "\"https://" + DEFAULT_HOSTNAME + "/a/different/path/\";"));
}

private String getFileContentsInZip(File zipFile, String path) throws IOException {
ZipFile zip = new ZipFile(zipFile);
InputStream is = zip.getInputStream(zip.getEntry(path));
return CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
try (ZipFile zip = new ZipFile(zipFile)) {
InputStream is = zip.getInputStream(zip.getEntry(path));
return CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2017 Google Inc.
*
* Licensed 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 com.google.cloud.tools.maven.endpoints.framework;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class ClientSrcMojoTest {

private static final String GEN_DIR_DEFAULT = "target/generated-sources/endpoints";
private static final String GEN_DIR_MODIFIED = "src-gen";
private static final String GENERATED_FILE_PATH = "/com/example/testApi/v1/TestApi.java";

@Rule public TemporaryFolder tmpDir = new TemporaryFolder();

private void buildAndVerify(File projectDir, String genDir) throws VerificationException {
Verifier verifier = new Verifier(projectDir.getAbsolutePath());
verifier.executeGoals(Arrays.asList("compile", "endpoints-framework:clientSrc"));
verifier.verifyErrorFreeLog();
verifier.assertFilePresent(genDir + GENERATED_FILE_PATH);
}

@Test
public void testDefault() throws XmlPullParserException, IOException, VerificationException {
File testDir = new TestProject(tmpDir.getRoot(), "/projects/server").build();

buildAndVerify(testDir, GEN_DIR_DEFAULT);
}

@Test
public void testGeneratedSrcDir()
throws XmlPullParserException, IOException, VerificationException {
File testDir =
new TestProject(tmpDir.getRoot(), "/projects/server")
.configuration(
"<configuration><generatedSrcDir>"
+ GEN_DIR_MODIFIED
+ "</generatedSrcDir></configuration>")
.build();

buildAndVerify(testDir, GEN_DIR_MODIFIED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;
import org.junit.rules.TemporaryFolder;

public class DiscoveryDocsMojoTest {
Expand All @@ -54,8 +53,9 @@ public void testDefault() throws IOException, VerificationException, XmlPullPars
File testDir = new TestProject(tmpDir.getRoot(), "/projects/server").build();
buildAndVerify(testDir);

String discovery = Files.toString(new File(testDir, DISCOVERY_DOC_PATH), Charsets.UTF_8);
Assert.assertThat(discovery, JUnitMatchers.containsString(DEFAULT_URL));
String discovery =
Files.asCharSource(new File(testDir, DISCOVERY_DOC_PATH), Charsets.UTF_8).read();
Assert.assertThat(discovery, CoreMatchers.containsString(DEFAULT_URL));
}

@Test
Expand All @@ -65,10 +65,11 @@ public void testApplicationId()
new TestProject(tmpDir.getRoot(), "/projects/server").applicationId("maven-test").build();
buildAndVerify(testDir);

String discovery = Files.toString(new File(testDir, DISCOVERY_DOC_PATH), Charsets.UTF_8);
Assert.assertThat(discovery, CoreMatchers.not(JUnitMatchers.containsString(DEFAULT_URL)));
String discovery =
Files.asCharSource(new File(testDir, DISCOVERY_DOC_PATH), Charsets.UTF_8).read();
Assert.assertThat(discovery, CoreMatchers.not(CoreMatchers.containsString(DEFAULT_URL)));
Assert.assertThat(
discovery, JUnitMatchers.containsString("https://maven-test.appspot.com/_ah/api"));
discovery, CoreMatchers.containsString("https://maven-test.appspot.com/_ah/api"));
}

@Test
Expand All @@ -79,9 +80,10 @@ public void testHostname() throws IOException, VerificationException, XmlPullPar
.build();
buildAndVerify(testDir);

String discovery = Files.toString(new File(testDir, DISCOVERY_DOC_PATH), Charsets.UTF_8);
Assert.assertThat(discovery, CoreMatchers.not(JUnitMatchers.containsString(DEFAULT_URL)));
Assert.assertThat(discovery, JUnitMatchers.containsString("https://my.hostname.com/_ah/api"));
String discovery =
Files.asCharSource(new File(testDir, DISCOVERY_DOC_PATH), Charsets.UTF_8).read();
Assert.assertThat(discovery, CoreMatchers.not(CoreMatchers.containsString(DEFAULT_URL)));
Assert.assertThat(discovery, CoreMatchers.containsString("https://my.hostname.com/_ah/api"));
}

@Test
Expand All @@ -92,8 +94,9 @@ public void testBasePath() throws IOException, VerificationException, XmlPullPar
.build();
buildAndVerify(testDir);

String openapi = Files.toString(new File(testDir, DISCOVERY_DOC_PATH), Charsets.UTF_8);
Assert.assertThat(openapi, CoreMatchers.not(JUnitMatchers.containsString(DEFAULT_BASE_PATH)));
Assert.assertThat(openapi, JUnitMatchers.containsString("\"basePath\": \"/a/different/path/"));
String openapi =
Files.asCharSource(new File(testDir, DISCOVERY_DOC_PATH), Charsets.UTF_8).read();
Assert.assertThat(openapi, CoreMatchers.not(CoreMatchers.containsString(DEFAULT_BASE_PATH)));
Assert.assertThat(openapi, CoreMatchers.containsString("\"basePath\": \"/a/different/path/"));
}
}
Loading

0 comments on commit 8d047e3

Please sign in to comment.