简体   繁体   English

从现有动态Web项目迁移到gradle

[英]Migrating from existing dynamic web project to gradle

I am having an existing java dynamic web project which I created using eclipse. 我正在使用eclipse创建的现有Java动态Web项目。 I usually just create a war file via eclipse and then deploy it to tomcat. 我通常只是通过eclipse创建一个war文件,然后将其部署到tomcat。

Now I want to use Gradle to build my project and create the war file. 现在我想使用Gradle构建我的项目并创建war文件。 Is there an eclipse plugin to do that? 有没有一个eclipse插件可以做到这一点? If not then how can I use gradle with my existing project? 如果没有,那么我如何在现有项目中使用gradle? 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. 我现有的项目结构是您通过eclipse创建动态Web项目时获得的结果,我不想更改它。

I have tried going through tutorials and using converting to Gradle project using gradle plugin. 我尝试过使用gradle插件进行教程并使用转换为Gradle项目。 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 : 我的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 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 我的proj结构 项目结构

Here's a minimal build.gradle file that should get you going. 这是一个最小的build.gradle文件,可以帮助你。 Build with the command gradle war . 使用命令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')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM