Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arquillian extension idea to use project artifact as deployment
package org.jboss.arquillian.extension.deployment.impl;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.maven.model.Model;
import org.jboss.arquillian.container.spi.client.deployment.DeploymentDescription;
import org.jboss.arquillian.container.test.spi.client.deployment.DeploymentScenarioGenerator;
import org.jboss.arquillian.extension.deployment.api.DeployProjectArtifact;
import org.jboss.arquillian.test.spi.TestClass;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.importer.ZipImporter;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.impl.maven.MavenDependencyResolverSettings;
import org.jboss.shrinkwrap.resolver.impl.maven.MavenRepositorySystem;
/**
* TODO get the build final name and the output directory from the Maven environment
*
* @author <a href="http://community.jboss.org/people/dan.j.allen">Dan Allen</a>
*/
public class ProjectArtifactDeploymentGenerator implements DeploymentScenarioGenerator {
@Override
public List<DeploymentDescription> generate(TestClass testClass) {
if (testClass.isAnnotationPresent(DeployProjectArtifact.class)) {
DeployProjectArtifact config = testClass.getAnnotation(DeployProjectArtifact.class);
String archiveName = config.value();
if (archiveName.length() == 0) {
archiveName = getArchiveNameFromPom();
}
Class<? extends Archive<?>> type = JavaArchive.class;
if (archiveName.endsWith(".war")) {
type = WebArchive.class;
}
else if (archiveName.endsWith(".ear")) {
type = EnterpriseArchive.class;
}
File archiveFile = new File("target/" + archiveName);
if (!archiveFile.exists()) {
throw new IllegalStateException("Project artifact has not been generated: " + archiveFile.getAbsolutePath());
}
Archive<?> archive = ShrinkWrap.create(type, archiveName).as(ZipImporter.class)
.importFrom(new File("target/" + archiveName)).as(type);
DeploymentDescription dd = new DeploymentDescription("application", archive);
dd.shouldBeTestable(config.testable());
return Arrays.asList(dd);
}
return Collections.emptyList();
}
// FIXME relying on internals of ShrinkWrap Maven Resolver
private String getArchiveNameFromPom() {
MavenRepositorySystem system = new MavenRepositorySystem();
MavenDependencyResolverSettings settings = new MavenDependencyResolverSettings();
Model model = system.loadPom(new File("pom.xml"), settings, system.getSession(settings));
String finalName = model.getBuild().getFinalName();
String packaging = model.getPackaging();
return finalName + '.' + packaging;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.