Skip to content

Commit 23f224f

Browse files
committed
#165: Also consider test dependencies for classpath of static analysis
1 parent d715ee6 commit 23f224f

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

cobigen-maven/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
44
<modelVersion>4.0.0</modelVersion>
55
<artifactId>cobigen-maven-plugin</artifactId>
6-
<version>1.2.0-SNAPSHOT</version>
6+
<version>1.1.1</version>
77
<packaging>maven-plugin</packaging>
88
<name>CobiGen Maven Plugin</name>
99

cobigen-maven/src/main/java/com/capgemini/cobigen/maven/GenerateMojo.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.ArrayList;
1515
import java.util.LinkedList;
1616
import java.util.List;
17+
import java.util.Set;
1718
import java.util.regex.Matcher;
1819

1920
import org.apache.maven.artifact.Artifact;
@@ -43,15 +44,16 @@
4344
import com.capgemini.cobigen.textmerger.TextMergerPluginActivator;
4445
import com.capgemini.cobigen.xmlplugin.XmlPluginActivator;
4546
import com.google.common.collect.Lists;
47+
import com.google.common.collect.Sets;
4648

4749
import freemarker.template.TemplateException;
4850

4951
/**
5052
* CobiGen generation Mojo, which handles generation using a configuration folder/archive
5153
* @author mbrunnli (08.02.2015)
5254
*/
53-
@Mojo(name = "generate", requiresDependencyResolution = ResolutionScope.RUNTIME, requiresProject = true,
54-
defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyCollection = ResolutionScope.RUNTIME)
55+
@Mojo(name = "generate", requiresDependencyResolution = ResolutionScope.TEST, requiresProject = true,
56+
defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyCollection = ResolutionScope.TEST)
5557
public class GenerateMojo extends AbstractMojo {
5658

5759
static {
@@ -333,13 +335,16 @@ private void generateFromTemplates(CobiGen cobiGen, List<Object> inputs) throws
333335
* @author mbrunnli (11.02.2015)
334336
*/
335337
private ClassLoader getProjectClassLoader() throws MojoFailureException {
336-
List<String> classpathElements = null;
338+
Set<String> classpathElements = Sets.newHashSet();
337339
try {
338-
classpathElements = project.getCompileClasspathElements();
340+
classpathElements.addAll(project.getCompileClasspathElements());
341+
classpathElements.addAll(project.getTestClasspathElements());
339342
List<URL> projectClasspathList = new ArrayList<>();
340343
for (String element : classpathElements) {
341344
try {
342-
projectClasspathList.add(new File(element).toURI().toURL());
345+
URL url = new File(element).toURI().toURL();
346+
getLog().debug("Add Classpath-URL: " + url);
347+
projectClasspathList.add(url);
343348
} catch (MalformedURLException e) {
344349
getLog().error(element + " is an invalid classpath element", e);
345350
throw new MojoFailureException(element + " is an invalid classpath element");

cobigen-maven/src/test/java/com/capgemini/cobigen/maven/systemtest/GenerateMojoTest.java

+32
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
import static org.junit.Assert.assertThat;
66

77
import java.io.File;
8+
import java.lang.reflect.Field;
89
import java.lang.reflect.Method;
910
import java.util.List;
1011

1112
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
13+
import org.apache.maven.project.DefaultMavenProjectBuilder;
14+
import org.apache.maven.project.DefaultProjectBuilderConfiguration;
15+
import org.apache.maven.project.MavenProject;
1216
import org.junit.Ignore;
1317
import org.junit.Test;
1418

@@ -44,4 +48,32 @@ public void testMojoPackageInputRetrieval() throws Exception {
4448

4549
assertThat(inputObjects.size(), equalTo(1));
4650
}
51+
52+
/**
53+
*
54+
* @throws Exception
55+
* @author mbrunnli (11.12.2015)
56+
*/
57+
@Test
58+
public void testResolveTestScopeClasspathResources() throws Exception {
59+
File testPom = new File("src/test/resources/testdata/systemtest/GenerateMojoTest/pom.xml");
60+
GenerateMojo mojo = (GenerateMojo) lookupMojo("generate", testPom);
61+
assertThat(mojo, notNullValue());
62+
63+
Method method = mojo.getClass().getDeclaredMethod("getProjectClassLoader");
64+
method.setAccessible(true);
65+
66+
Field field = mojo.getClass().getDeclaredField("project");
67+
field.setAccessible(true);
68+
MavenProject build =
69+
new DefaultMavenProjectBuilder().build(testPom, new DefaultProjectBuilderConfiguration());
70+
field.set(mojo, build);
71+
72+
@SuppressWarnings("unchecked")
73+
ClassLoader classLoader = (ClassLoader) method.invoke(mojo);
74+
75+
// Assert: should not throw a ClassNotFoundException
76+
classLoader.loadClass("org.custommonkey.xmlunit.XMLTestCase");
77+
78+
}
4779
}

0 commit comments

Comments
 (0)