Configuring the Maven Project for Endpoints
Before adding code and testing it, you need to make sure the project has all the necessary dependencies and project settings. Make sure you complete all of the configurations described below.
Adding the Project ID to appengine-web.xml
To add the project ID:
- Navigate to the project in
helloendpoints/helloendpoints-war. - Open the file
src/main/webapp/WEB-INF/appengine-web.xmlin an editor. - In the
<application>field, copy the project ID (App Engine app ID). This is the value you obtained following the Setup instructions to create the project. (The backend API will be deployed to this project ID.) - In the
<version>field, set the value to1. (For more information about deploying to application versions, see Deploying to multiple App Engine app versions.) - Save your work and close the file.
Configuring web.xml
To configure web.xml:
- Locate the file
helloendpoints/helloendpoints-war/src/main/webapp/WEB-INF/web.xmland open it in an editor. -
Replace the file contents with the following:
<?xml version="1.0" encoding="utf-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>SystemServiceServlet</servlet-name> <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class> <init-param> <param-name>services</param-name> <param-value>com.google.appengine.samples.helloendpoints.Greetings</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>SystemServiceServlet</servlet-name> <url-pattern>/_ah/spi/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
Setting required properties entries
To set the properties:
- Navigate to the
helloendpoints/helloendpoints-wardirectory, and open thepom.xmlfile with an editor. - Locate the
<properties>entries in the defaultpom.xmlfile: -
In
<properties>, locate the line<appengine.app.version>version</appengine.app.version>, and add the line<appengine.target.version><!--version--></appengine.target.version>replacing
<!--Latest App Engine Java SDK version-->with the latest App Engine Java SDK version: 1.9.1. When finished, the entries should look something like this:<modelVersion>4.0.0</modelVersion> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <groupId>com.google.appengine.samples.helloendpoints</groupId> <artifactId>helloendpoints</artifactId> <properties> <appengine.app.version>1</appengine.app.version> <appengine.target.version><!--Latest App Engine Java SDK version--></appengine.target.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> -
Make sure that
<packaging>is set towar. - Keep the
pom.xmlfile open and set dependencies configuration as described in the next section.
Setting required runtime dependencies
To set dependencies:
- In
pom.xml, locate the dependencies under the commentCompile/runtime dependencies. -
Replace all of those entries with the following:
<!-- Compile/runtime dependencies --> <dependency> <groupId>com.google.appengine</groupId> <artifactId>appengine-api-1.0-sdk</artifactId> <version>${appengine.target.version}</version> </dependency> <dependency> <groupId>com.google.appengine</groupId> <artifactId>appengine-endpoints</artifactId> <version>${appengine.target.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>Notice in the above lines that
javax.injectis not absolutely required, but it is commonly used, since any parameter injection using @Named in Endpoints must usejavax.injectfor this. It is required for this tutorial. -
Keep the
pom.xmlfile open and set plugin configuration as described in the next sections.
Setting required plugin configuration
To set the default plugin configuration:
-
In
pom.xml, locate and replace the defaultmaven-war-pluginconfiguration entirely with the following:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <webXml>${project.build.directory}/generated-sources/appengine-endpoints/WEB-INF/web.xml</webXml> <webResources> <resource> <!-- this is relative to the pom.xml directory --> <directory>${project.build.directory}/generated-sources/appengine-endpoints</directory> <!-- the list has a default value of ** --> <includes> <include>WEB-INF/*.discovery</include> <include>WEB-INF/*.api</include> </includes> </resource> </webResources> </configuration> </plugin> -
Locate and replace the default
appengine-maven-pluginconfiguration entirely with the following:<plugin> <groupId>com.google.appengine</groupId> <artifactId>appengine-maven-plugin</artifactId> <version>${appengine.target.version}</version> <configuration> <enableJarClasses>false</enableJarClasses> </configuration> <executions> <execution> <goals> <goal>endpoints_get_discovery_doc</goal> </goals> </execution> </executions> </plugin> -
Save your work and close the file.
-
Rebuild the project to make sure everything is working as expected, as described in the next section.
Rebuilding the project with the new pom.xml settings
When you finish modifying the pom.xml file, rebuild the project by invoking
Maven as follows:
mvn install
Next...
At this point, you are ready to write backend code.
Continue to Writing the API: A Simple GET.