Build.gradle with tomcat (8.5.4) in idea

Hello,

I build a small servlet with Idea and tomcat. Initally the project started out with what looks like maven so I removed the iml file and dropped in a build.gradle which looks like:

group 'importer.com
version ‘1.0-SNAPSHOT’

apply plugin: 'groovy’
apply plugin: 'java’
apply plugin: 'idea’
apply plugin: 'application’
apply plugin: ‘war’

sourceCompatibility = 1.7

repositories {
mavenCentral()
}

dependencies {
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
compile group: ‘com.fasterxml.jackson.core’, name: ‘jackson-databind’, version: ‘2.8.5’
}

The problem is that if I try to test this file tomcat seems to report that it starts ok:

24-Feb-2017 11:06:18.498 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 42 ms
24-Feb-2017 11:06:28.506 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\webapps\manager
24-Feb-2017 11:06:28.891 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\webapps\manager has finished in 369 ms

But if I try to browse to localhost:8080 I get the google chrome page not found (not the usual tomcat page not found).

I am pretty new to all this stuff and not sure whether my issue lies with my idea project setup or the build.gradle.

Any help appreciated.

Regards,

Al

I assume you are trying to setup a Tomcat configuration in IntelliJ to execute your build script? I don’t think IntelliJ actually evaluates your build script logic to run Tomcat. Can you provide the full source code on GitHub so I can reproduce it?

I am also a little bit confused why you’d want to apply the application plugin if you want to build and run a web application. Can you explain?

If I’d approach the problem you are trying to solve then I’d not mess around with IntelliJ at all. I would apply the Gradle Tomcat plugin and just start Tomcat from the command line.

Hi Ben,

Yes I am trying to setup a tomcat configuration from IntelliJ (and it was working before I converted to to a gradle project - I think it was maven previously as I removed a iml file).

I can send you privately the source code.

I have removed the application plugin and I would be perhaps even more confused if I tried to do this from CLI.

Here is a log trying to start it from IntelliJ:

C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\bin\catalina.bat run
[2017-02-24 11:32:37,550] Artifact poltest:warexploded: Server is not connected. Deploy is not available.
Using CATALINA_BASE: "C:\Users\AlGrant.IntelliJIdea2016.3\system\tomcat\Tomcat_8_5_4_poltest"
Using CATALINA_HOME: "C:\Users\AlGrant\Documents\apache-tomcat-8.5.4"
Using CATALINA_TMPDIR: "C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0_101"
Using CLASSPATH: "C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\bin\bootstrap.jar;C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\bin\tomcat-juli.jar"
24-Feb-2017 11:32:38.429 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.4
24-Feb-2017 11:32:38.430 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jul 6 2016 08:43:30 UTC
24-Feb-2017 11:32:38.430 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.4.0
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Windows 10
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 10.0
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: C:\Program Files\Java\jdk1.8.0_101\jre
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_101-b13
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: C:\Users\AlGrant.IntelliJIdea2016.3\system\tomcat\Tomcat_8_5_4_poltest
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: C:\Users\AlGrant\Documents\apache-tomcat-8.5.4
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote=
24-Feb-2017 11:32:38.431 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.port=1099
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.ssl=false
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcom.sun.management.jmxremote.authenticate=false
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.rmi.server.hostname=127.0.0.1
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\AlGrant.IntelliJIdea2016.3\system\tomcat\Tomcat_8_5_4_poltest\conf\logging.properties
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\AlGrant.IntelliJIdea2016.3\system\tomcat\Tomcat_8_5_4_poltest
24-Feb-2017 11:32:38.432 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=C:\Users\AlGrant\Documents\apache-tomcat-8.5.4
24-Feb-2017 11:32:38.433 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\temp
24-Feb-2017 11:32:38.433 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.8.0_101\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Java\jre1.8.0_101\bin;C:\Users\AlGrant\gradle-3.1\bin;C:\Program Files\nodejs;C:\Users\AlGrant\AppData\Roaming\npm\node_modules\htmlhint\lib;C:\Program Files\MySQL\MySQL Utilities 1.6;C:\Program Files (x86)\Skype\Phone;C:\Users\AlGrant\AppData\Local\Microsoft\WindowsApps;C:\Users\AlGrant\AppData\Roaming\npm;.
24-Feb-2017 11:32:38.564 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler [“http-nio-8080”]
24-Feb-2017 11:32:38.625 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
24-Feb-2017 11:32:38.628 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler [“ajp-nio-8009”]
24-Feb-2017 11:32:38.630 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
24-Feb-2017 11:32:38.630 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 506 ms
24-Feb-2017 11:32:38.670 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
24-Feb-2017 11:32:38.670 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.4
24-Feb-2017 11:32:38.682 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [http-nio-8080]
24-Feb-2017 11:32:38.691 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [ajp-nio-8009]
24-Feb-2017 11:32:38.693 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 61 ms
Connected to server
[2017-02-24 11:32:39,198] Artifact poltest:warexploded: Artifact is being deployed, please wait…
[2017-02-24 11:32:39,259] Artifact poltest:warexploded: Error during artifact deployment. See server log for details.
[2017-02-24 11:32:39,261] Artifact poltest:warexploded: com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException: C:\Users\AlGrant\IdeaProjects\poltest\out\artifacts\poltest_warexploded\poltest_warexploded.war not found for the web module.
24-Feb-2017 11:32:48.683 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\webapps\manager
24-Feb-2017 11:32:48.967 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\webapps\manager has finished in 284 ms

Looks like a issue finding the exploded war?

HTH

-Al

I think it would be easier for other contributors and users on the forum if you could just push your code to GitHub instead of sharing it privately.

Some observations about your code:

  1. The build fails at the moment. You still define mainClassName even though the application plugin is not applied.
  2. Your Java source code does not reside in the conventional Java source directory and therefore is not compiled. You’ll need to move it under src/main/java.
  3. The class name you use does not follow usual Java conventions. It should start with an upper-case letter.
  4. All the source code you have under web should be moved under src/main/webapp.

Points 2 and 4 are likely the root cause of your problem.

Hi Ben,

I have moved the java src code to be under /src/main/webapp

I have fixed the build.gradle:

group 'poltest’
version ‘1.0-SNAPSHOT’

apply plugin: 'groovy’
apply plugin: 'java’
apply plugin: 'idea’
apply plugin: ‘war’

sourceCompatibility = 1.7

repositories {
mavenCentral()
}

dependencies {
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
compile group: ‘com.fasterxml.jackson.core’, name: ‘jackson-databind’, version: ‘2.8.5’
}

and now the gradle task to build war seems ok:

1:41:08 PM: Executing external task ‘war’…
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:war UP-TO-DATE

BUILD SUCCESSFUL

Total time: 4.782 secs
1:41:13 PM: External task execution finished ‘war’.

However if I run tomcat and look at /manager there is no poltest listed.

I wouldn’t count on the manager app to be there. I am not even sure if IntelliJ even installs the manager application by default. I’d rather try to get to your application. Do you even see compiled classes under build/classes/main now? If you don’t then you still have an issue with where you put your Java source files. Try to reach the context of your application.

Keep in mind, IntelliJ might handle the execution of your web application completely different from your Gradle setup. How important is it to you to get your web application running from the IDE?

It would be nice to at least have the IDE launch tomcat and then I could type in the context URL into the browser.

Are you saying I should launch tomcat from CLI then browse to URL?

I can’t see a /classes/main folder in my project structure:

IntelliJ compiles code to the out folder where as Gradle compiles code to src/main/java. I am not sure how good the integration of IntelliJ is in regards to picking up the correct classes and resources when starting a servlet container like Tomcat. It simply might not work at all. That’s why I am suggesting to completely rely on Gradle for that purpose and simply use a Gradle plugin to bring up the container. It would mean starting Tomcat from the command line with a task instead of from the IDE. That would at least unblock you and we could work on the IDE integration as the next thing.

BTW: Your code can’t possibly compile as you do not define a dependency on the Servlet API.

Before we continue with this please upload your code to GitHub so we can properly collaborate. Go ahead and obfuscate your code as needed.

Hi Ben,

My project structure (and file path) does not have a /src/main/java - it does have /src/main/webapp

I managed to publish the project to Git:

https://github.com/bigalnz/ptest

I can not find a war when I run the gradle task to build the war on my disk (I tried to search for it) - there is nothing in the task output that shows the build failed:

8:57:10 AM: Executing external task ‘war’…
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:war UP-TO-DATE

BUILD SUCCESSFUL

Total time: 5.998 secs
8:57:24 AM: External task execution finished ‘war’.

Thanks in advance

Al

I also noted this:

It complains about the lack of Manifest.mf - which is actually present.

If I point Idea at that file - it then wants a Main class - but there is not one for a web app?

I have also noted that if I use the war task no war seems to be created.

If i use the Idea task to build the war i can manually copy it to webapps and start tomcat and I see it deployed, but when I browse to the context I get 404.

I looked in the expanded webapps folder for poltest and I notice there are no files containing the static content that I wanted my servlet to serve up on a Get.

So idea is not including the index.html which is under \WEB\ ?

Does this help?

AL

I touched up your project and got it working in a Tomcat instance declared through IntelliJ. For the purpose I sent you a pull request: https://github.com/bigalnz/ptest/pull/1. Just merge it and you should be fine. A lot of the issue you are facing involve around the basic knowledge about Gradle conventions. Please read the following pages to get up to speed:

To get the web application started in the IDE:

  1. Generate the project files via ./gradlew idea
  2. Open the project file ptest.ipr.
  3. Generate the web application archive with ./gradlew build.
  4. Configure Tomcat and point to the generated archive under build/libs.
  5. Start Tomcat. Your index page and your /form page should work.

As you can see we are using a lot of the files that have been generated by the Gradle build and not IntelliJ. That’s what I meant with interoperability issues earlier. I am sure you can also configure the web artifact in IntelliJ and use that. However, that’s more a question for JetBrains. I am not sure where they stand with the functionality.

Sometimes the Gretty plugin works for me to run jetty,tomcat etc, so i put it in a diff gradle script like gretty.gradle.

@jnorthr What exactly do you mean by “sometimes”?

Hi Ben,

Thank you. I have created a project from scratch using the IDE and, then the gradle plugin in the IDE to create most of the project structure and then added the html files and java classes. It compiled and worked as expected.

I copied the war to my tomcat installation, but this will quickly become tedious so I will configure tomcat to look in build/libs.

Thanks again,

-Al

In steps 1 and 2 these commands are issued through the CLI (I am on windows) but did you create the project first in idea and cd to that before issuing these commands? I got:

FAILURE: Build failed with an excpetion
*What went wrong:
Task ‘idea’ not found in root project “myname”

Cheers

Al

I created the project files with the Idea plugin from the command line. I added that in my pull request or it was already there. Either way both ways should work.

Yeah I seem to be able to do it reliably now.

Do I need any special options to the build.gradle to be able to debug this code by using a remote debugger?

I have managed to connect to the remote debugger on tomcat but the war is not stopping at my breakpoints.

Regards,

Al

You will likely need to add the remote debugging options to the Tomcat process. Everything else should work as usual. The configuration is not really relevant to Gradle as you not running the Gradle process in this case. I’ll let the community take it from here. Maybe you can also ask at JetBrain’s support site.

A colleague mentioned that in order for remote debugging to work:

you will need to dig out the src and test-source folders from build.gradle and specifically configure your IntelliJ project to point to:

IntelliJ source folder will need to be exact location of source folder from build.gradle

IntelliJ test-source folder will need to be exact location of test folder from build.gradle

I don’t have either of these even in my gradle.