Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-5044] catch FileNotFoundException if mojos are execute…
…d from a classes directory instead from a jar
  • Loading branch information
kutzi committed May 22, 2011
1 parent 78c709e commit 68f5a0d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -66,6 +66,9 @@
<li class=bug>
Allow building multiple downstream dependencies on a single job via DependencyGraph and BuildTrigger.
(<a href="http://issues.jenkins-ci.org/browse/JENKINS-8985">issue 8985</a>)
<li class=bug>
Catch FileNotFoundException in Maven builds if Mojos are executed from a classes directory.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-5044">issue 5044</a>)
<li class=rfe>
Set NODE_NAME for master node to "master"
(<a href="http://issues.jenkins-ci.org/browse/JENKINS-9671">issue 9671</a>)
Expand Down
7 changes: 7 additions & 0 deletions maven-plugin/pom.xml
Expand Up @@ -308,6 +308,13 @@ THE SOFTWARE.
<version>1.1</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
6 changes: 5 additions & 1 deletion maven-plugin/src/main/java/hudson/maven/ExecutedMojo.java
Expand Up @@ -30,6 +30,7 @@
import hudson.util.ReflectionUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -94,12 +95,15 @@ public ExecutedMojo(MojoInfo mojo, long duration) throws IOException, Interrupte
MojoDescriptor md = mojo.mojoExecution.getMojoDescriptor();
PluginDescriptor pd = md.getPluginDescriptor();
try {
Class clazz = getMojoClass( md, pd );// pd.getClassRealm().loadClass(md.getImplementation());
Class<?> clazz = getMojoClass( md, pd );
digest = Util.getDigestOf(new FileInputStream(Which.jarFile(clazz)));
} catch (IllegalArgumentException e) {
LOGGER.log(Level.WARNING, "Failed to locate jar for "+md.getImplementation(),e);
} catch (ClassNotFoundException e) {
// perhaps the plugin has failed to load.
} catch (FileNotFoundException e) {
// Maybe mojo was loaded from a classes dir instead of from a jar (JENKINS-5044)
LOGGER.log(Level.WARNING, "Failed to caculate digest for "+md.getImplementation(),e);
}
this.digest = digest;
}
Expand Down
62 changes: 62 additions & 0 deletions maven-plugin/src/test/java/hudson/maven/ExecutedMojoTest.java
@@ -0,0 +1,62 @@
package hudson.maven;

import java.io.IOException;

import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;

/**
* Unit test for {@link ExecutedMojo}.
*
* @author kutzi
*/
public class ExecutedMojoTest {

private MojoDescriptor mojoDescriptor;

@Before
public void before() {
PluginDescriptor pluginDescriptor = new PluginDescriptor();
pluginDescriptor.setGroupId("com.test");
pluginDescriptor.setArtifactId("testPlugin");
pluginDescriptor.setVersion("1.0");

ClassRealm classRealm = new ClassRealm(null, "test", getClass().getClassLoader());
pluginDescriptor.setClassRealm(classRealm);

MojoDescriptor descriptor = new MojoDescriptor();
descriptor.setPluginDescriptor(pluginDescriptor);
this.mojoDescriptor = descriptor;
}

@Test
public void testMojoFromJarFile() throws IOException, InterruptedException {
// Faking JUnit's Assert to be the plugin class
this.mojoDescriptor.setImplementation(Assert.class.getName());
MojoExecution execution = new MojoExecution(this.mojoDescriptor);
MojoInfo info = new MojoInfo(execution, null, null, null);

ExecutedMojo executedMojo = new ExecutedMojo(info, 1L);

Assert.assertNotNull(executedMojo.digest);
}

@Test
@Bug(5044)
public void testMojoFromClassesDirectory() throws IOException, InterruptedException {
// Faking this class as the mojo impl:
this.mojoDescriptor.setImplementation(getClass().getName());
MojoExecution execution = new MojoExecution(this.mojoDescriptor);
MojoInfo info = new MojoInfo(execution, null, null, null);

ExecutedMojo executedMojo = new ExecutedMojo(info, 1L);

Assert.assertEquals("com.test", executedMojo.groupId);
}
}

0 comments on commit 68f5a0d

Please sign in to comment.