Skip to content

Commit

Permalink
SHRINKRES-194 Multi-module Gradle project support
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatloka committed Aug 15, 2014
1 parent 1238c27 commit b14a8ad
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
@@ -0,0 +1,5 @@
apply plugin: 'maven'

group = 'org.jboss.shrinkwrap.resolver.test'
version = '1.0.0'
description = """ShrinkWrap Resolver Maven Archie Implementation Tests: Multi-Module Sample"""
@@ -0,0 +1,13 @@
apply plugin: 'java'

repositories {
mavenCentral()
}

dependencies {
compile 'commons-codec:commons-codec:1.7'
testCompile 'junit:junit:4.10'
compile('org.jboss.spec:jboss-javaee-web-6.0:3.0.2.Final') {
exclude(module: 'xalan')
}
}
@@ -0,0 +1,13 @@
package test;

import javax.ejb.Stateless;

@Stateless
public class JarClass {

public static final String GREETINGS = "Hello from EmbeddedGradleImporter imported class";

public String greet() {
return GREETINGS;
}
}
@@ -0,0 +1,10 @@
apply plugin: 'java'
apply plugin: 'war'

repositories {
mavenCentral()
}

dependencies {
compile(project(':module-one'))
}
@@ -0,0 +1,2 @@
include "module-one"
include "module-two"
Expand Up @@ -24,6 +24,7 @@
import org.gradle.tooling.BuildLauncher;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.model.DomainObjectSet;
import org.gradle.tooling.model.GradleProject;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Assignable;
Expand All @@ -50,6 +51,8 @@ public class EmbeddedGradleImporterImpl implements EmbeddedGradleImporter, Distr

private ProjectConnection projectConnection;

private String projectName;

private BuildLauncher getBuildLauncher() {
if (buildLauncher == null) {
projectConnection = connector.connect();
Expand All @@ -73,6 +76,7 @@ public DistributionConfigurationStage forProjectDirectory(final File projectDir)
throw new IllegalArgumentException("Given project dir is not a directory" + absoluteFile);
}

projectName = absoluteFile.getName();
connector.forProjectDirectory(absoluteFile);
return this;
}
Expand All @@ -91,20 +95,55 @@ public DistributionConfigurationStage forThisProjectDirectory() {

@Override
public <TYPE extends Assignable> TYPE as(final Class<TYPE> clazz) {
final GradleProject gradleProject = projectConnection.getModel(GradleProject.class);

final File buildDir = gradleProject.getBuildDirectory();
final GradleProject currentGradleProject = findCurrentGradleProject();
final File buildDir = currentGradleProject.getBuildDirectory();
final File libsDir = new File(buildDir, "libs");
final File result = libsDir.listFiles(new FilenameFilter() {
final File[] results = libsDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(final File dir, final String name) {
return name.startsWith(gradleProject.getName());
return name.startsWith(currentGradleProject.getName());
}
})[0];
});

if (results.length == 0) {
throw new IllegalArgumentException(
"Wrong project directory is used. Tests have to be run from working directory which is a current sub-module directory.");
}

final File result = results[0];
return ShrinkWrap.create(ZipImporter.class, archive.getName()).importFrom(result).as(clazz);
}

private GradleProject findCurrentGradleProject() {
final GradleProject rootGradleProject = projectConnection.getModel(GradleProject.class);
if (rootGradleProject.getName() != projectName) {
final GradleProject child = findChildProject(rootGradleProject, projectName);
if (child != null) {
return child;

}
}
return rootGradleProject;
}

private GradleProject findChildProject(GradleProject gradleProject, String childProjectName) {
final DomainObjectSet<? extends GradleProject> children = gradleProject.getChildren();
for (GradleProject child : children) {
if (child.getName().equals(childProjectName)) {
return child;
}
}

for (GradleProject child : children) {
final GradleProject foundChild = findChildProject(child, childProjectName);
if (foundChild != null) {
return foundChild;
}
}

return null;
}

@Override
public ConfigurationStage useGradleVersion(final String version) {
connector.useGradleVersion(version);
Expand Down
@@ -0,0 +1,21 @@
package org.jboss.shrinkwrap.impl.gradle.archive.importer.embedded;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.gradle.archive.importer.embedded.EmbeddedGradleImporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;

/**
* @author <a href="mailto:mmatloka@gmail.com">Michal Matloka</a>
*/
public class MultiModuleEmbeddedGradleImporterTestCase {

@Test
public void should() {
final String dir = "src/it/multi-module-sample/module-two";
final WebArchive webArchive = ShrinkWrap.create(EmbeddedGradleImporter.class).forProjectDirectory(dir)
.importBuildOutput().as(WebArchive.class);

AssertArchive.assertContains(webArchive, "/WEB-INF/lib/module-one.jar");
}
}

0 comments on commit b14a8ad

Please sign in to comment.