Populating the classpath in an existing maven project

72 views
Skip to first unread message

David Michael Gang

unread,
Apr 21, 2015, 8:31:17 AM4/21/15
to node...@googlegroups.com
Hi all,

I am working on a existing maven project with a pom.xml.
I want to use classes of this maven project.
Using node-java-maven is not so easy because my dependencies are already specified and i don't want to duplicate them.
How can i populate my classpath?

One option i thought is to populate the classpath from the .classpath file which is of the form

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
  <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
  <classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>
  <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
  <classpathentry kind="output" path="target/classes"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/4.8.2/junit-4.8.2.jar" sourcepath="M2_REPO/junit/junit/4.8.2/junit-4.8.2-sources.jar">
    <attributes>
      <attribute value="jar:file:/C:/Users/davidga/.m2/repository/junit/junit/4.8.2/junit-4.8.2-javadoc.jar!/" name="javadoc_location"/>
    </attributes>
  </classpathentry>
</classpath>

This would mean that i have to parse the classpath xml file and push the result to the java classpath.
This looks a bit tedious.

Is there a better solution?

Thanks,
David

Jim Lloyd

unread,
Apr 22, 2015, 4:48:31 PM4/22/15
to David Michael Gang, node...@googlegroups.com
My knowledge of maven is fairly superficial, but we use it to install .jar files into the target directory (and target/dependency) and then set up the java classpath by using glob to enumerate all of those jars. Is there a reason you can't do that?

--
You received this message because you are subscribed to the Google Groups "node-java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-java+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Michael Gang

unread,
Apr 26, 2015, 4:21:05 AM4/26/15
to node...@googlegroups.com, micha...@gmail.com
Hi,

I am editing also the java classes, so i don't want on each step to install the jar file again and again.

I found an additional solution.
I issue a
> mvn dependency:copy-dependencies
command

I wrote then a class to configure the java class:

"use strict";
var fs = require("fs");
var java = require("java");
var baseDir = "./target/dependency";
var dependencies = fs.readdirSync(baseDir);

dependencies.forEach(function(dependency){
    java.classpath.push(baseDir + "/" + dependency);
})

java.classpath.push("./target/classes");
java.classpath.push("./target/test-classes");

exports.getJavaInstance = function() {
    return java;
}

and then in the consuming class i write:
var javaInit = require('./javaInit');
var java = javaInit.getJavaInstance();


Thanks,
David

Joe Ferner

unread,
Apr 26, 2015, 8:09:41 AM4/26/15
to node...@googlegroups.com
David, that's how I have my code structured as well. Work well like that.
Reply all
Reply to author
Forward
0 new messages