Debugging Google App Engine app in Intellij

2019-04-15 12:07发布

I'm going through the "Hello World" tutorial from Google App Engine for Java. Everything works fine when I run the app in my browser by executing:

mvn appengine:devserver

In my browser I just launch:

http://localhost:8080

But in Intellij, when I run Debug, the browser opens but the page cannot be displayed (Error 403).

I believe the problem has to do with the way the project is configured in Intellij. See the attached screenshot of my Debug settings:

Debug settings

It isn't clear to me what application server the Maven command is using when I type it in and which one is used when I run it from Intellij. The Intellij does appear to use Jetty. Here is a snapshot of the Application Server settings in Intellij:

App Server Settings

Here's the pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015 Google Inc. All Rights Reserved.

 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.
-->
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <groupId>com.example.appengine</groupId>
    <artifactId>appengine-helloworld</artifactId>
    <!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
    <parent>
        <groupId>com.google.cloud</groupId>
        <artifactId>doc-samples</artifactId>
        <version>1.0.0</version>
        <relativePath>../..</relativePath>
    </parent>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <!-- for hot reload of the web application -->
        <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <version>3.3</version>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.google.appengine</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>${appengine.sdk.version}</version>
            </plugin>
        </plugins>
    </build>
</project>

How can I fix this so that I can run Debug in Intellij?

1条回答
干净又极端
2楼-- · 2019-04-15 12:14

You are running your dev server with Maven, so you need to setup a Remote debug configuration.

  • Under the Run menu, select Edit configurations...
  • Click the + icon (top left) to add a new configuration
  • Select Remote. You should see these options

Remote debug options for App Engine

You will see it is listening on port 5005 so you need to make sure the App Engine plugin section in your pom.xml has this section with the same port number:

<plugin>
    <groupId>com.google.appengine</groupId>
    <artifactId>appengine-maven-plugin</artifactId>
    <version>${appengine.sdk.version}</version>
    <configuration>
        <-- other config stuff here -->
        <jvmFlags>
            <jvmFlag>-Xdebug</jvmFlag>
            <jvmFlag>-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005</jvmFlag>
        </jvmFlags>
    </configuration>
</plugin>

You start the dev server as normal (on port 8080 or whatever you configured in your pom), but now you can put a breakpoint in your code and when you trigger that piece of code (e.g., going to the page in your browser), you can step through in the debugger.

查看更多
登录 后发表回答