eclipse で Gradle プロジェクトの Spring 環境をつくる(part 2)

こんにちわ、平和な日々を過ごすほげPGです。
今回は、前回Eclipse上で設定したことを build.gradle に記載してEclipseプロジェクトを作成したいと思います。
手順は空のフォルダに以下の build.gradle を作成し、gradle eclipseを実行するだけです。
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
sourceCompatibility = 1.8
targetCompatibility = 1.8
war {
archiveName 'hoge.war'
destinationDir file('build')
}
task prepareMyProject << {
file('src/main/java').mkdirs()
file('src/main/resources').mkdirs()
file('src/main/webapp').mkdirs()
}
eclipseClasspath.dependsOn prepareMyProject
task wrapper(type: Wrapper) {
gradleVersion = '2.14.1'
}
repositories {
jcenter()
}
dependencies {
compile 'org.springframework:spring-webmvc:4.3.7.RELEASE'
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'org.slf4j:slf4j-log4j12:1.7.21'
providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
providedCompile 'javax.servlet:jsp-api:2.0'
testCompile 'junit:junit:4.12'
}
eclipse {
project {
buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
natures 'org.eclipse.buildship.core.gradleprojectnature'
}
classpath {
defaultOutputDir = file('build/classes')
file {
whenMerged { classpath ->
classpath.entries.removeAll { it.kind == "lib" }
classpath.entries.removeAll { it.kind == "con" }
}
withXml {
def node = it.asNode()
node.appendNode( 'classpathentry', [ kind: 'con', path: 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'])
node.appendNode( 'classpathentry', [ kind: 'con', path: 'org.eclipse.buildship.core.gradleclasspathcontainer', exported: 'true'])
.appendNode('attributes')
.appendNode('attribute', [name: 'org.eclipse.jst.component.dependency', value: '/WEB-INF/lib'])
}
}
}
wtp {
facet {
facet name: 'jst.web', version: '3.0'
facet name: 'java', version: '1.8'
}
component {
contextPath = 'hoge'
file {
whenMerged { modules ->
modules.wbModuleEntries.removeAll { it instanceof org.gradle.plugins.ide.eclipse.model.WbDependentModule }
}
}
}
}
}
tasks.eclipse.doFirst {
File buildship_prefs = file(".settings/org.eclipse.buildship.core.prefs")
buildship_prefs.append('''
build.commands=org.eclipse.jdt.core.javabuilder,org.eclipse.wst.common.project.facet.core.builder,org.eclipse.wst.validation.validationbuilder
connection.arguments=
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.java.home=null
connection.jvm.arguments=
connection.project.dir=
derived.resources=.gradle,build
eclipse.preferences.version=1
natures=org.eclipse.jdt.core.javanature,org.eclipse.wst.common.project.facet.core.nature,org.eclipse.wst.common.modulecore.ModuleCoreNature,org.eclipse.jem.workbench.JavaEMFNature
project.path=\\:
'''. stripIndent())
}
説明:
war {
archiveName 'hoge.war'
destinationDir file('build')
}
war生成時の設定です。
archiveName:warファイル名
destinationDir:war出力先フォルダ
task prepareMyProject << {
file('src/main/java').mkdirs()
file('src/main/resources').mkdirs()
file('src/main/webapp').mkdirs()
}
eclipseClasspath.dependsOn prepareMyProject
空フォルダの作成です。
task wrapper(type: Wrapper) {
gradleVersion = '2.14.1'
}
ラッパー用のタスクです。gradlewを使いたい場合はgradle wrapper を実行してください。
eclipse {
project {
buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
natures 'org.eclipse.buildship.core.gradleprojectnature'
}
gradleプロジェクトにする設定です。
classpath {
defaultOutputDir = file('build/classes')
file {
whenMerged { classpath ->
classpath.entries.removeAll { it.kind == "lib" }
classpath.entries.removeAll { it.kind == "con" }
}
withXml {
def node = it.asNode()
node.appendNode( 'classpathentry', [ kind: 'con', path: 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'])
node.appendNode( 'classpathentry', [ kind: 'con', path: 'org.eclipse.buildship.core.gradleclasspathcontainer', exported: 'true'])
.appendNode('attributes')
.appendNode('attribute', [name: 'org.eclipse.jst.component.dependency', value: '/WEB-INF/lib'])
}
}
}
.classpathの設定です。defaultOutputDirは出力先です。
<classpathentry kind=”lib” …> と<classpathentry kind=”con” …>をいったん削除し、必要なxmlタグを追加。
wtp {
facet {
facet name: 'jst.web', version: '3.0'
facet name: 'java', version: '1.8'
}
ファセットの設定です。
component {
contextPath = 'hoge'
file {
whenMerged { modules ->
modules.wbModuleEntries.removeAll { it instanceof org.gradle.plugins.ide.eclipse.model.WbDependentModule }
}
}
}
コンテキストパスの設定と、アセンブリ上の不要な指定の削除です。
tasks.eclipse.doFirst {
File buildship_prefs = file(".settings/org.eclipse.buildship.core.prefs")
buildship_prefs.append('''
build.commands=org.eclipse.jdt.core.javabuilder,org.eclipse.wst.common.project.facet.core.builder,org.eclipse.wst.validation.validationbuilder
connection.arguments=
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.java.home=null
connection.jvm.arguments=
connection.project.dir=
derived.resources=.gradle,build
eclipse.preferences.version=1
natures=org.eclipse.jdt.core.javanature,org.eclipse.wst.common.project.facet.core.nature,org.eclipse.wst.common.modulecore.ModuleCoreNature,org.eclipse.jem.workbench.JavaEMFNature
project.path=\\:
'''. stripIndent())
}
org.eclipse.buildship.core.prefsが作成されず、ビルドエラーになってしまったので、ファイルを勝手に作成する設定です。中身は前回作成したプロジェクトのorg.eclipse.buildship.core.prefsの内容そのままです。
今回は以上です。ではでは。
1 thoughts on “eclipse で Gradle プロジェクトの Spring 環境をつくる(part 2)”
この投稿はコメントできません。