0

I have a Gradle webapp having plugin war and jetty defined in build.gradle, as follows:

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jetty'

group = 'com.company'
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.0'
war.baseName = 'deploy'

repositories {
    mavenLocal()
    mavenCentral()
    maven { url "http://repository.pentaho.org/artifactory/repo/" }
}
dependencies {
    ..
}

When I perform a gradle build with jettyRunWar, as follows:

$ ./gradlew jettyRunWar

It build the application and deploy it to Jetty as expected.

Now I want to deploy the same application to Tomcat7 as well, to do the same, I ran the gradle war task as follows:

$ ./gradlew war

after running the task, I can see the deploy-1.0.war inside /Users/ArpitAggarwal/deploy/build/libs/ and when I tried to deploy it to Tomcat7, it's not picked up by Tomcat.

My question is - Do I need to apply any additional plugin to make the application deployable to Tomcat7?

Thanks.

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • No, You don't need any additional plugins, you should be able to deploy the same war to tomcat. Do you see any errors in tomcat startup?? I just created a war and deployed it to tomcat and it worked fine. – Nighthacks Dec 26 '15 at 08:32
  • No errors in tomcat logs. Tomcat started fine without booting `.war`, I think Tomcat is not aware about `.war` file somehow. I tried with Eclipse as well, firstly "Add Web Module" is disabled and when I tried to add it using "Add External Web Module", it not picking up. – Arpit Aggarwal Dec 26 '15 at 08:38
  • I too deploy wars generated by gradle war plug-in to tomcat regularly. But gradle war plug-in does not do it for me. It just produces a war file in my build directory. I use bash or other tools to deploy, and with Tomcat default installation you can just copy the war into .../webapps/ and let Tomcat deploy it. There is a tomcat plug-in for gradle intended to deploy war files. There may be other ways to do it. – joshp Dec 26 '15 at 08:40
  • Ohh, i thought you are deploying it manually. `war` plug-in does not do that, it only creates a war file. So either you will have to deploy it manually or add some other plugin. – Nighthacks Dec 26 '15 at 08:53
  • I tried deploying manually only but starting the server from Eclipse and `.war` was not picked up by Tomcat(not sure why), then I tried starting the server from command which works. But this give rise to another problem, for which I am writing an answer along with yours. – Arpit Aggarwal Dec 26 '15 at 09:32
  • By manually means you need to copy it to tomcat webapps folder. – Nighthacks Dec 26 '15 at 09:43
  • Exactly, I copied it to webapps folder. – Arpit Aggarwal Dec 26 '15 at 09:46

2 Answers2

1

war plug-in only creates a war, it doesn't deploy generated war to tomcat by itself.

You can create a task to deploy war to tomcat.

task deployToTomcat(type: Copy) {
    from war.archivePath
    into "${tomcatHome}/webapps"
}

You can also use gradle-tomcat-plugin.

Nighthacks
  • 410
  • 1
  • 4
  • 13
-1

The problem is contextRoot of .war file. Gradle generates the contextRoot of .war file picking the war.baseName and version, so the contextRoot is changed to deploy-1.0 and the application is accessible at:

localhost:8080/deploy-1.0

Then I removed the version field from build.gradle which makes Gradle generate .war with contextRoot as deploy and application accessibility at:

localhost:8080/deploy/
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108