On 23/05/07, Lasse Koskela <[EMAIL PROTECTED]> wrote:

Oops. Sorry about the ambiguous title in my previous post... I changed
it to something more communicative.

Lasse

On 5/23/07, Lasse Koskela <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I'm trying to convert the open source JUnit extension called JspTest
> to use Maven2 for the build and I'm running into trouble with how
> Maven's Surefire test runner plugin seems to "hide" the classpath from
> the executing code.
>
> The issue stems from the need for a unit test to be able to compile
> Java source code (generated from JSP source files), which obviously
> needs all sorts of J2EE stuff in the classpath. These dependencies
> have been defined in the pom.xml and are visible just fine when
> compiling the framework itself and when running the tests, but when
> those tests (indirectly) attempt to use the same dependencies to build
> up a classpath for a new "javac" process I get all kinds of
> "symbol/package/class not found" compilation errors because
> System.getProperty("java.class.path") returns only some
> maven/surefire/plexus libraries and not a single dependency (not even
> the "target/classes" or "target/test-classes" directories).
>
> If I were talking about a plugin, I could have the dependencies be
> injected to the Mojo through properties but I'm not talking about a
> plugin so that's not an option.
>
> So, I guess my question is how can I get access to the Maven
> dependencies and/or the full classpath?
>


Hi Lasse,

I was hoping to see an answer to this since I had a similar issue. I have a
class that executes processes. I wanted to write tests for this class, to
ensure that it does what I think it does, and make various assertions about
the exit status and stdout / stderr of the process being tested.

Within Eclipse, the System property java.class.path works well, but not in
maven2. So I resorted to manually constructing the classpath myself. Not
ideal, but I didn't see a system property or similar with the full test
classpath available.

   public class ClasspathBuilder {

       /**
        * Path to maven repository.
        */
       private final String mavenRepository;

       /**
        * Classpath being constructed.
        */
       private StringBuffer classpath;

       /**
        * The base directory used to build a new entry.
        */
       private String basedir;

       /**
        * Create a new ClasspathBuilder.
        *
        */
       public ClasspathBuilder() {
           mavenRepository = joinPaths(joinPaths(System
                   .getProperty("user.home"), ".m2"), "repository");
           basedir = mavenRepository;
           classpath = new StringBuffer(".");
       }

       /**
        * Set the base directory.
        *
        * @param files
        *            an array of files consituting the base directory
        */
       public void setBaseDir(String[] files) {
           StringBuffer buf = new StringBuffer(files[0]);

           for (int i = 1, n = files.length; i < n; ++i) {
               buf.append(joinPaths(buf.toString(), files[i]));
           }
           basedir = buf.toString();
       }

       /**
        * Create a new Entry.
        *
        * @param files
        *            string array of file names, which will be the
directories
        *            in the path below the $MAVEN_HOME/repository
        */
       public void addEntry(String[] files) {
           StringBuffer entry = new StringBuffer();
           entry.append(basedir);
           for (int i = 0, n = files.length; i < n; ++i) {
               entry.append(File.separatorChar);
               entry.append(files[i]);
           }
           classpath.append(File.pathSeparatorChar);
           classpath.append(entry.toString());
       }

       /**
        * Join the paths.
        *
        * @param root
        *            the root path.
        * @param file
        *            the file / directory to append.
        * @return return the newly extended path
        */
       private String joinPaths(String root, String file) {
           StringBuffer buf = new StringBuffer(root);
           buf.append(File.separatorChar);
           buf.append(file);
           return buf.toString();
       }

       /**
        * Return the constructed classpath.
        *
        * @return the classpath
        */
       public String getClasspath() {
           return classpath.toString();
       }

       /**
        * [EMAIL PROTECTED]
        */
       public String toString() {
           return getClasspath();
       }

   }


and using it like this:

       /*
        * This only works within the IDE - at least within Eclipse
        */
       // return System.getProperty("java.class.path");
       ClasspathBuilder builder = new ClasspathBuilder();
       builder.addEntry(new String[] { "commons-io", "commons-io", "1.3.1",
               "commons-io-1.3.1.jar" });
       builder.addEntry(new String[] { "commons-lang", "commons-lang", "2.0
",
               "commons-lang-2.0.jar" });
       builder.addEntry(new String[] { "log4j", "log4j", "1.2.8",
               "log4j-1.2.8.jar" });

       builder.setBaseDir(new String[] { System.getProperty("user.dir") });
       builder.addEntry(new String[] { "my-module", "target",
               "test-classes" });
       builder.addEntry(new String[] { "target", "test-classes" });
       return builder.getClasspath();


Not ideal, but it's not giving me any pain thus far. Obviously, when
dependency versions change, I have a minor maintenance issue, but it works
for me currently.

Cheers,

James


> best regards,
> Lasse

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to