
org.bluestemsoftware.open.eoa.plugin.compile.UnitTestMojo Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2008 Bluestem Software LLC. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*/
package org.bluestemsoftware.open.eoa.plugin.compile;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.resolver.ArtifactCollector;
import org.apache.maven.plugin.MojoExecutionException;
import org.bluestemsoftware.open.eoa.plugin.util.ClasspathHelper;
import org.bluestemsoftware.open.eoa.plugin.util.DependencyHelper;
/**
* Compiles unit test sources. Note that plexus component descriptor included within plugin
* dist binds test-compile phase within default lifecycle to this mojo's test-compile goal.
*
* @goal test-compile
* @phase test-compile
*/
public class UnitTestMojo extends AbstractCompilerMojo {
static final String LINE_BREAK = System.getProperty("line.separator");
/**
* Collects artifacts.
*
* @component
*/
private ArtifactCollector artifactCollector;
/**
* For retrieval of artifact's metadata.
*
* @component
*/
private ArtifactMetadataSource metadataSource;
public UnitTestMojo() {
super();
}
public void execute() throws MojoExecutionException, CompilationFailureException {
if (skip) {
getLog().info("tests skipped. not compiling sources");
return;
}
super.execute();
}
protected List getCompileSourceRoots() {
File sourceRoot = new File(basedir, "src/test/java/");
return Collections.singletonList(sourceRoot.getAbsolutePath());
}
protected File getOutputDirectory() {
return new File(basedir, "target/test-classes/");
}
protected List getClasspathElements() throws MojoExecutionException {
Set pd = null;
try {
pd = DependencyHelper.getProjectDependencies(project, artifactFactory);
} catch (Exception ex) {
throw new MojoExecutionException("Error retrieving project dependencies. " + ex);
}
ClasspathHelper classpathHelper = new ClasspathHelper(logger, basedir, project, false);
classpathHelper.setArtifactCollector(artifactCollector);
classpathHelper.setArtifactFactory(artifactFactory);
classpathHelper.setArtifactResolver(artifactResolver);
classpathHelper.setLocalRepository(localRepository);
classpathHelper.setMetadataSource(metadataSource);
classpathHelper.setRemoteRepositories(remoteRepositories);
classpathHelper.setSystemTestClassesDirectory(getOutputDirectory());
Map urls = new HashMap();
Set versionlessKeys = new HashSet();
urls.putAll(classpathHelper.getParentClasspath(pd, versionlessKeys));
urls.putAll(classpathHelper.getSharedClasspath(pd, versionlessKeys, true));
urls.putAll(classpathHelper.getDeploymentClasspath(pd, versionlessKeys));
urls.putAll(classpathHelper.getSystemTestClasspath(pd, versionlessKeys));
if (logger.isDebugEnabled()) {
logger.debug(LINE_BREAK);
logger.debug("classpath: ");
for (Map.Entry entry : urls.entrySet()) {
logger.debug("constituent: "
+ new File(entry.getKey()).getName()
+ " dependency trail: "
+ entry.getValue());
}
logger.debug("end classpath");
}
// strip scheme and decode each url before returning as
// an element within -classpath argument
List constituents = new ArrayList();
for (String url : urls.keySet()) {
try {
constituents.add(new URL(url).toURI().getPath());
} catch (Exception fatchance) {
}
}
return constituents;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy