8

I am having an existing java dynamic web project which I created using eclipse. I usually just create a war file via eclipse and then deploy it to tomcat.

Now I want to use Gradle to build my project and create the war file. Is there an eclipse plugin to do that? If not then how can I use gradle with my existing project? My existing project structure is what you get when you create a dynamic web project via eclipse and I don't want to change it.

I have tried going through tutorials and using converting to Gradle project using gradle plugin. Can someone atleast point to a blog or tutorial or a way to start things?

   MyProject
    |java resources
    |--src
    |-- -- packages
    |-- -- .properties files
    |--test
    |-- -- packages
    |build
    |-- classes
    |WebContent
    |-- MetaINF
    |-- WEB-INF
    |-- -- lib // all my libraries are here. there is no specific repo for now
    |-- -- web.xml

My build.gradle :

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'war'


sourceCompatibility = 1.7
targetCompatibility = 1.7


version = '2.0'

war {

baseName = 'Gradle'
version = '1.2'
}
repositories {
mavenCentral()
}

repositories {
flatDir {
   dirs 'lib'
   }
}

dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'

compile files('./lib/myjar.jar')    
compile 'commons-codec:commons-codec:1.5'

compile 'commons-lang:commons-lang:2.6'
compile 'org.hibernate:hibernate-entitymanager:4.3.8.Final'
compile 'org.apache.axis2:axis2-kernel:1.6.2'
compile 'org.apache.axis2:axis2-adb:1.6.2'
compile 'com.sun.jersey:jersey-server:1.19'
compile 'com.sun.jersey:jersey-client:1.19'
compile 'log4j:log4j:1.2.17'
compile 'org.apache.axis2:axis2-transport-local:1.6.2'
compile 'org.apache.axis2:axis2-transport-http:1.6.2'
providedCompile 'javax.servlet:servlet-api:2.3'



testCompile 'junit:junit:4.9'
testCompile 'org.jmock:jmock:2.6.0'
testCompile 'org.jmock:jmock-junit4:2.6.0'
testCompile 'org.jmock:jmock-legacy:2.6.0'



}

test {
systemProperties 'property': 'value'
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name>Gradle</display-name>
<servlet>
    <servlet-name>HTTP REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    //in the above line It is showing a warning: servlet-class references to non-existent class "com.sun.jersey.spi.container.servlet.ServletContainer"
    <init-param>
       <param-name>com.sun.jersey.config.property.packages</param-name>
       <param-value>org.gradle</param-value>
   </init-param>

    <load-on-startup>1</load-on-startup>


</servlet>
<servlet-mapping>
    <servlet-name>HTTP REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

My proj structure Project Structure

Jenny
  • 345
  • 3
  • 5
  • 15
  • Can you describe the layout out your source files in more detail? – dnault Apr 08 '15 at 15:54
  • In particular, where does your static web content live? Where is the Java source code? Are resources like *.properties files stored in a separate directory, or in the same directory as the Java source? Where is the web.xml file? Does your project depend on other JARs, and if so are they available in an artifact repository or are they only available on the local filesystem? – dnault Apr 08 '15 at 16:00
  • please check. I have updated my question with dir structure and also my project is dependent on another project and jar is also in lib folder. – Jenny Apr 08 '15 at 16:12

1 Answers1

8

Here's a minimal build.gradle file that should get you going. Build with the command gradle war.

apply plugin: 'war'

// See http://gradle.org/docs/current/userguide/war_plugin.html
//   Section 26.5. Convention properties
webAppDirName = 'WebContent'

// See http://gradle.org/docs/current/userguide/java_plugin.html
//   Section 23.4.1. Changing the project layout
sourceSets {
    main {
        // where does the Java source code live?
        java {
            srcDir 'Java Resources/src'
        }

        // where do classpath resources like *.properties files live?
        resources {
            srcDir 'Java Resources/src'
        }
    }
}

// See http://gradle.org/docs/current/userguide/dependency_management.html
//   Section 51.4.4. File dependencies
dependencies {
    // Where do the JARs live on the filesystem?
    compile fileTree(dir: "${webAppDirName}/WEB-INF/lib", include: '*.jar')
}
dnault
  • 8,340
  • 1
  • 34
  • 53
  • Hi, Thanks. But I have started building the project from scratch... Now I ve added all the dependencies and repository as mavenCentral(). Now only problem is that Test task is failing. Exception it shows is `org.apache.axis2.deployment.DeploymentException: org.apache.axis2.transport. local.LocalTransportSender at org.apache.axis2.deployment.AxisConfigBuilder.processTransportSenders (AxisConfigBuilder.java:707)` – Jenny Apr 08 '15 at 20:30
  • That's great -- fetching dependencies from an artifact repository is the way to go. I'd suggest checking to make sure all the required Axis libraries are present (and maybe also the servlet API as a providedCompile dependency?) I'm not an Axis user, so I'm afraid that's all the help I can offer. – dnault Apr 08 '15 at 20:48
  • Yep. Will check that. Thanks though! As I am new to Gradle, I ll work on your answer too :) – Jenny Apr 08 '15 at 21:07
  • Hi Again! I have done everything.. even my build is successful and i can see a war file now. But gradle is not locating web.xml file i guess. Cos its not showing the display name when I am deploying that war file in tomcat. Can you please help me with that? I will update my question with my web.xml and also build file. – Jenny Apr 09 '15 at 23:33
  • Also in web.xml it is showing one warning. I have commented that warning in web.xml. – Jenny Apr 09 '15 at 23:44