【问题标题】:Migrating from existing dynamic web project to gradle从现有的动态 Web 项目迁移到 gradle
【发布时间】:2015-06-13 15:57:18
【问题描述】:

我有一个使用 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>

我的项目结构

【问题讨论】:

  • 你能更详细地描述你的源文件的布局吗?
  • 特别是,您的静态 Web 内容在哪里? Java源代码在哪里? *.properties 文件之类的资源是存储在单独的目录中,还是与 Java 源存储在同一目录中? web.xml 文件在哪里?您的项目是否依赖于其他 JAR,如果是,它们在工件存储库中可用还是仅在本地文件系统上可用?
  • 请检查。我已经用 dir 结构更新了我的问题,而且我的项目依赖于另一个项目,并且 jar 也在 lib 文件夹中。

标签: java eclipse tomcat gradle gradle-eclipse


【解决方案1】:

这是一个最小的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)
  • 太好了——从工件存储库中获取依赖项是可行的方法。我建议检查以确保所有必需的 Axis 库都存在(也许还有 servlet API 作为 providedCompile 依赖项?)我不是 Axis 用户,所以恐怕这就是我能提供的所有帮助。
  • 是的。会检查的。不过谢谢!因为我是 Gradle 新手,所以我也会为您解答:)
  • 您好!我已经完成了一切..即使我的构建成功,我现在可以看到一个战争文件。但我猜 gradle 没有找到 web.xml 文件。因为当我在 tomcat 中部署该战争文件时,它没有显示显示名称。你能帮我吗?我将使用我的 web.xml 更新我的问题并构建文件。
  • 也在 web.xml 中显示一个警告。我已经在 web.xml 中评论了这个警告。
猜你喜欢
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 2016-07-29
  • 1970-01-01
  • 2019-02-07
  • 2015-02-13
  • 1970-01-01
  • 2011-10-13
相关资源
最近更新 更多