Gradle: fix compatibility with Gradle 2.0

This commit is contained in:
Vladislav.Soroka
2014-06-25 16:12:48 +04:00
parent cbfc524435
commit 84cd6169d7
6 changed files with 148 additions and 145 deletions
@@ -19,6 +19,7 @@
<root url="jar://$MODULE_DIR$/../lib/gradle-messaging-1.12.jar!/" />
<root url="jar://$MODULE_DIR$/../lib/gradle-wrapper-1.12.jar!/" />
<root url="jar://$MODULE_DIR$/../lib/gradle-base-services-1.12.jar!/" />
<root url="jar://$MODULE_DIR$/../lib/gradle-base-services-groovy-1.12.jar!/" />
<root url="jar://$MODULE_DIR$/../lib/gradle-native-1.12.jar!/" />
</CLASSES>
<JAVADOC />
@@ -0,0 +1,140 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.gradle.tooling.builder
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.FileVisitDetails
import org.gradle.api.file.FileVisitor
import org.gradle.api.java.archives.Manifest
import org.gradle.api.plugins.WarPlugin
import org.gradle.api.tasks.bundling.War
import org.gradle.util.GradleVersion
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
import org.jetbrains.plugins.gradle.model.web.WebConfiguration
import org.jetbrains.plugins.gradle.tooling.ErrorMessageBuilder
import org.jetbrains.plugins.gradle.tooling.ModelBuilderService
import org.jetbrains.plugins.gradle.tooling.internal.web.WarModelImpl
import org.jetbrains.plugins.gradle.tooling.internal.web.WebConfigurationImpl
import org.jetbrains.plugins.gradle.tooling.internal.web.WebResourceImpl
/**
* @author Vladislav.Soroka
* @since 6/25/2014
*/
class WarModelBuilderImpl implements ModelBuilderService {
private static final String WEB_APP_DIR_PROPERTY = "webAppDir"
private static final String WEB_APP_DIR_NAME_PROPERTY = "webAppDirName"
@Override
public boolean canBuild(String modelName) {
return WebConfiguration.name.equals(modelName)
}
@Nullable
@Override
public Object buildAll(String modelName, Project project) {
final WarPlugin warPlugin = project.plugins.findPlugin(WarPlugin)
if (warPlugin == null) return null
final String webAppDirName = !project.hasProperty(WEB_APP_DIR_NAME_PROPERTY) ?
"src/main/webapp" : String.valueOf(project.property(WEB_APP_DIR_NAME_PROPERTY))
final File webAppDir = !project.hasProperty(WEB_APP_DIR_PROPERTY) ? new File(project.projectDir, webAppDirName) :
(File)project.property(WEB_APP_DIR_PROPERTY)
def warModels = []
project.tasks.each { Task task ->
if (task instanceof War) {
final WarModelImpl warModel =
new WarModelImpl((task as War).archiveName, webAppDirName, webAppDir)
final List<WebConfiguration.WebResource> webResources = []
final War warTask = task as War
warModel.webXml = warTask.webXml
warTask.rootSpec.walk({ def resolver ->
// def resolver ->
// in Gradle v1.x - org.gradle.api.internal.file.copy.CopySpecInternal
// in Gradle v2.x - org.gradle.api.internal.file.copy.CopySpecResolver
if (resolver.metaClass.respondsTo(resolver, 'setIncludeEmptyDirs', boolean)) {
resolver.setIncludeEmptyDirs(true)
}
if (!resolver.metaClass.respondsTo(resolver, 'getDestPath') ||
!resolver.metaClass.respondsTo(resolver, 'getSource')) {
throw new RuntimeException("${GradleVersion.current()} is not supported by web artifact importer")
}
final String relativePath = resolver.destPath.pathString
resolver.source.visit(new FileVisitor() {
@Override
public void visitDir(FileVisitDetails dirDetails) {
try {
addPath(webResources, relativePath, dirDetails.path, dirDetails.file)
}
catch (Exception ignore) {
}
}
@Override
public void visitFile(FileVisitDetails fileDetails) {
try {
if (warTask.webXml == null ||
!fileDetails.file.canonicalPath.equals(warTask.webXml.canonicalPath)) {
addPath(webResources, relativePath, fileDetails.path, fileDetails.file)
}
}
catch (Exception ignore) {
}
}
})
})
warModel.webResources = webResources
warModel.classpath = warTask.classpath.files
Manifest manifest = warTask.manifest
if (manifest != null) {
def writer = new StringWriter()
manifest.writeTo(writer)
warModel.manifestContent = writer.toString()
}
warModels.add(warModel)
}
}
new WebConfigurationImpl(warModels)
}
@NotNull
@Override
public ErrorMessageBuilder getErrorMessageBuilder(@NotNull Project project, @NotNull Exception e) {
ErrorMessageBuilder.create(
project, e, "Web project import errors"
).withDescription("Web Facets/Artifacts will not be configured")
}
private static addPath(List<WebConfiguration.WebResource> webResources, String warRelativePath, String fileRelativePath, File file) {
warRelativePath = warRelativePath == null ? "" : warRelativePath
WebConfiguration.WebResource webResource = new WebResourceImpl(warRelativePath, fileRelativePath, file)
webResources.add(webResource)
}
}
@@ -1,140 +0,0 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.gradle.tooling.builder;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.file.FileVisitDetails;
import org.gradle.api.file.FileVisitor;
import org.gradle.api.internal.file.copy.CopySpecInternal;
import org.gradle.api.java.archives.Manifest;
import org.gradle.api.plugins.WarPlugin;
import org.gradle.api.tasks.bundling.War;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.gradle.model.web.WebConfiguration;
import org.jetbrains.plugins.gradle.tooling.ErrorMessageBuilder;
import org.jetbrains.plugins.gradle.tooling.ModelBuilderService;
import org.jetbrains.plugins.gradle.tooling.internal.web.WarModelImpl;
import org.jetbrains.plugins.gradle.tooling.internal.web.WebConfigurationImpl;
import org.jetbrains.plugins.gradle.tooling.internal.web.WebResourceImpl;
import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
/**
* @author Vladislav.Soroka
* @since 11/5/13
*/
public class WarModelBuilderImpl implements ModelBuilderService {
private static final String WEB_APP_DIR_PROPERTY = "webAppDir";
private static final String WEB_APP_DIR_NAME_PROPERTY = "webAppDirName";
@Override
public boolean canBuild(String modelName) {
return WebConfiguration.class.getName().equals(modelName);
}
@Nullable
@Override
public Object buildAll(String modelName, Project project) {
final WarPlugin warPlugin = project.getPlugins().findPlugin(WarPlugin.class);
if (warPlugin == null) return null;
final String webAppDirName = !project.hasProperty(WEB_APP_DIR_NAME_PROPERTY) ?
"src/main/webapp" : String.valueOf(project.property(WEB_APP_DIR_NAME_PROPERTY));
final File webAppDir = !project.hasProperty(WEB_APP_DIR_PROPERTY)
? new File(project.getProjectDir(), webAppDirName)
: (File)project.property(WEB_APP_DIR_PROPERTY);
List<WebConfiguration.WarModel> warModels = new ArrayList<WebConfiguration.WarModel>();
for (Task task : project.getTasks()) {
if (task instanceof War) {
final WarModelImpl warModel =
new WarModelImpl(((War)task).getArchiveName(), webAppDirName, webAppDir);
final List<WebConfiguration.WebResource> webResources = new ArrayList<WebConfiguration.WebResource>();
final War warTask = (War)task;
warModel.setWebXml(warTask.getWebXml());
warTask.getRootSpec().walk(new Action<CopySpecInternal>() {
@Override
public void execute(CopySpecInternal internal) {
final String relativePath = internal.getDestPath().getPathString();
internal.getSource().visit(new FileVisitor() {
@Override
public void visitDir(FileVisitDetails dirDetails) {
try {
addPath(webResources, relativePath, dirDetails.getPath(), dirDetails.getFile());
}
catch (Exception ignore) {
}
}
@Override
public void visitFile(FileVisitDetails fileDetails) {
try {
if (warTask.getWebXml() == null ||
!fileDetails.getFile().getCanonicalPath().equals(warTask.getWebXml().getCanonicalPath())) {
addPath(webResources, relativePath, fileDetails.getPath(), fileDetails.getFile());
}
}
catch (Exception ignore) {
}
}
});
}
});
warModel.setWebResources(webResources);
warModel.setClasspath(warTask.getClasspath().getFiles());
Manifest manifest = warTask.getManifest();
if (manifest != null) {
StringWriter writer = new StringWriter();
manifest.writeTo(writer);
warModel.setManifestContent(writer.toString());
}
warModels.add(warModel);
}
}
return new WebConfigurationImpl(warModels);
}
@NotNull
@Override
public ErrorMessageBuilder getErrorMessageBuilder(@NotNull Project project, @NotNull Exception e) {
return ErrorMessageBuilder.create(
project, e, "Web project import errors"
).withDescription("Web Facets/Artifacts will not be configured");
}
private static void addPath(List<WebConfiguration.WebResource> webResources, String warRelativePath, String fileRelativePath, File file) {
warRelativePath = warRelativePath == null ? "" : warRelativePath;
WebConfiguration.WebResource webResource = new WebResourceImpl(warRelativePath, fileRelativePath, file);
webResources.add(webResource);
}
}
@@ -20,7 +20,7 @@ allprojects {
idea {
module {
scopes.PROVIDED.plus += configurations.provided
scopes.PROVIDED.plus += [configurations.provided]
}
}
}
@@ -65,6 +65,7 @@ public abstract class AbstractModelBuilderTest {
public static final String GRADLE_v1_10 = "1.10";
public static final String GRADLE_v1_11 = "1.11";
public static final String GRADLE_v1_12 = "1.12";
public static final String GRADLE_v2_0 = "2.0-rc-2";
public static final Pattern TEST_METHOD_NAME_PATTERN = Pattern.compile("(.*)\\[(\\d*)\\]");
@@ -84,10 +85,11 @@ public abstract class AbstractModelBuilderTest {
@Parameterized.Parameters
public static Collection<Object[]> data() {
Object[][] data = {
{AbstractModelBuilderTest.GRADLE_v1_9},
{AbstractModelBuilderTest.GRADLE_v1_10},
{AbstractModelBuilderTest.GRADLE_v1_11},
{AbstractModelBuilderTest.GRADLE_v1_12}
{GRADLE_v1_9},
{GRADLE_v1_10},
{GRADLE_v1_11},
{GRADLE_v1_12},
{GRADLE_v2_0}
};
return Arrays.asList(data);
}