Skip to content

Commit

Permalink
Merge pull request #73 from jglick/IDEA-is-broken-JENKINS-45245
Browse files Browse the repository at this point in the history
[JENKINS-45245] Work around failure of IDEA to add jenkins-war.war to the test CP
  • Loading branch information
jglick committed Sep 20, 2017
2 parents 2a12efb + 509c27f commit b45ae9b
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/main/java/org/jvnet/hudson/test/WarExploder.java
Expand Up @@ -23,13 +23,15 @@
*/ */
package org.jvnet.hudson.test; package org.jvnet.hudson.test;


import hudson.remoting.Which;
import hudson.FilePath; import hudson.FilePath;

import hudson.remoting.Which;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;


/** /**
* Ensures that <tt>jenkins.war</tt> is exploded. * Ensures that <tt>jenkins.war</tt> is exploded.
Expand All @@ -42,6 +44,8 @@
*/ */
public final class WarExploder { public final class WarExploder {


private static final Logger LOGGER = Logger.getLogger(WarExploder.class.getName());

public static File getExplodedDir() throws Exception { public static File getExplodedDir() throws Exception {
// rethrow an exception every time someone tries to do this, so that when explode() // rethrow an exception every time someone tries to do this, so that when explode()
// fails, you can see the cause no matter which test case you look at. // fails, you can see the cause no matter which test case you look at.
Expand Down Expand Up @@ -74,18 +78,31 @@ private static File explode() throws Exception {
if(new File(d,".jenkins").exists()) { if(new File(d,".jenkins").exists()) {
File dir = new File(d,"war/target/jenkins"); File dir = new File(d,"war/target/jenkins");
if(dir.exists()) { if(dir.exists()) {
System.out.println("Using jenkins.war resources from "+dir); LOGGER.log(Level.INFO, "Using jenkins.war resources from {0}", dir);
return dir; return dir;
} }
} }
} }


// locate jenkins.war // locate jenkins.war
File war;
URL winstone = WarExploder.class.getResource("/winstone.jar"); URL winstone = WarExploder.class.getResource("/winstone.jar");
if(winstone==null) if (winstone != null) {
// impossible, since the test harness pulls in jenkins.war war = Which.jarFile(Class.forName("executable.Executable"));
throw new AssertionError("jenkins.war is not in the classpath. If you are running this from the core workspace, run 'mvn install' to create the war image in war/target/jenkins. If running from a plugin, try using `mvn clean test` (cf. JENKINS-45245)."); } else {
File war = Which.jarFile(Class.forName("executable.Executable")); // JENKINS-45245: work around incorrect test classpath in IDEA. Note that this will not correctly handle timestamped snapshots; in that case use `mvn test`.
File core = Which.jarFile(Jenkins.class); // will fail with IllegalArgumentException if have neither jenkins-war.war nor jenkins-core.jar in ${java.class.path}
String version = core.getParentFile().getName();
if (core.getName().equals("jenkins-core-" + version + ".jar") && core.getParentFile().getParentFile().getName().equals("jenkins-core")) {
war = new File(new File(new File(core.getParentFile().getParentFile().getParentFile(), "jenkins-war"), version), "jenkins-war-" + version + ".war");
if (!war.isFile()) {
throw new AssertionError(war + " does not yet exist. Prime your development environment by running `mvn validate`.");
}
LOGGER.log(Level.FINE, "{0} is the continuation of the classpath by other means", war);
} else {
throw new AssertionError(core + " is not in the expected location, and jenkins-war-*.war was not in " + System.getProperty("java.class.path"));
}
}


// TODO this assumes that the CWD of the Maven process is the plugin ${basedir}, which may not be the case // TODO this assumes that the CWD of the Maven process is the plugin ${basedir}, which may not be the case
File buildDirectory = new File(System.getProperty("buildDirectory", "target")); File buildDirectory = new File(System.getProperty("buildDirectory", "target"));
Expand All @@ -97,7 +114,7 @@ private static File explode() throws Exception {
File timestamp = new File(explodeDir,".timestamp"); File timestamp = new File(explodeDir,".timestamp");


if(!timestamp.exists() || (timestamp.lastModified()!=war.lastModified())) { if(!timestamp.exists() || (timestamp.lastModified()!=war.lastModified())) {
System.out.println("Exploding " + war + " into " + explodeDir); LOGGER.log(Level.INFO, "Exploding {0} into {1}", new Object[] {war, explodeDir});
new FileOutputStream(explodeDir + ".exploding").close(); new FileOutputStream(explodeDir + ".exploding").close();
new FilePath(explodeDir).deleteRecursive(); new FilePath(explodeDir).deleteRecursive();
new FilePath(war).unzip(new FilePath(explodeDir)); new FilePath(war).unzip(new FilePath(explodeDir));
Expand All @@ -107,9 +124,12 @@ private static File explode() throws Exception {
timestamp.setLastModified(war.lastModified()); timestamp.setLastModified(war.lastModified());
new File(explodeDir + ".exploding").delete(); new File(explodeDir + ".exploding").delete();
} else { } else {
System.out.println("Picking up existing exploded jenkins.war at "+explodeDir.getAbsolutePath()); LOGGER.log(Level.INFO, "Picking up existing exploded jenkins.war at {0}", explodeDir.getAbsolutePath());
} }


return explodeDir; return explodeDir;
} }

private WarExploder() {}

} }

0 comments on commit b45ae9b

Please sign in to comment.