从现有的动态Web项目迁移到Gradle

8
我有一个使用Eclipse创建的Java动态Web项目。我通常通过Eclipse创建war文件,然后将其部署到Tomcat。现在我想使用Gradle构建我的项目并创建war文件。是否有Eclipse插件可以做到这一点?如果没有,那么我该如何在现有项目中使用Gradle?我的现有项目结构是通过Eclipse创建动态Web项目时得到的,我不想改变它。我已经尝试查看教程并使用Gradle插件将其转换为Gradle项目。可以有人指向一个博客、教程或开始的方法吗?
   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

我的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>

我的项目结构 项目结构


你能更详细地描述一下你的源文件布局吗? - dnault
特别是,你的静态网页内容放在哪里?Java源代码在哪里?像*.properties文件这样的资源是存储在单独的目录中还是与Java源代码存储在同一个目录中?web.xml文件在哪里?您的项目是否依赖其他JAR文件,如果是的话,它们是在构件存储库中可用还是仅在本地文件系统中可用? - dnault
请检查。我已更新我的问题,包括目录结构,而且我的项目依赖于另一个项目,jar文件也在lib文件夹中。 - Jenny
1个回答

8

这是一个最小的build.gradle文件,可以让您开始构建。使用命令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')
}

嗨,谢谢。但我已经从头开始构建项目了...现在我已经添加了所有依赖项和仓库作为mavenCentral()。现在唯一的问题是测试任务失败了。它显示的异常是org.apache.axis2.deployment.DeploymentException: org.apache.axis2.transport.local.LocalTransportSender at org.apache.axis2.deployment.AxisConfigBuilder.processTransportSenders(AxisConfigBuilder.java:707) - Jenny
太好了——从构件库获取依赖项是正确的方法。我建议检查是否存在所有必需的Axis库(也许还有作为providedCompile依赖项的servlet API?)我不是一个Axis用户,所以恐怕这就是我能提供的所有帮助了。 - dnault
好的,我会检查一下。谢谢你!由于我对Gradle还不熟悉,我也会参考你的答案进行学习的 :) - Jenny
你好!我已经完成了所有的工作,甚至我的构建也成功了,现在我可以看到一个war文件。但是我猜gradle没有找到web.xml文件,因为当我在tomcat中部署该war文件时,它没有显示名称。你能帮我解决这个问题吗?我会更新我的问题,并提供我的web.xml和构建文件。 - Jenny
同时在web.xml中显示了一个警告。我已经在web.xml中注释掉了那个警告。 - Jenny

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接