Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add classpath to netbeans maven project

I have a java project which uses .properties files for configuration. On the server, when launching, I set the classpath to include a folder which contains all the properties files. On my local machine, I'd like to point to a different folder.

I'm looking to add to the classpath, ideally for all projects, but adding it to each project is also fine. I've tried changing the Run > VM Options to include classpath, but with that change it can't find the main class, and I get java.lang.NoClassDefFoundError. I've also tried changing nbactions.xml directly to set the classpath to -classpath ~\MyFolder\;%classpath, but this has the same problem.

To add to the difficulty, the server is running linux while my local machine is running Windows.

like image 346
Dave Avatar asked Nov 13 '13 11:11

Dave


1 Answers

I stuck with topic-starter issue also for a long time. My goal - put config files for debug purpose in project root and extend classpath to ${basedir}, so this code:

String appConfigLocation = System.getProperty("config.location");
if (appConfigLocation == null) {
    logger.error("System property 'config.location' is not set...");
    System.exit(1);
}
InputStream appConfigStream = Main.class.getClassLoader().getResourceAsStream(appConfigLocation);
if (appConfigStream == null) {
    logger.error("Can't find resource {} in classpath, fix 'config.location'...", appConfigLocation);
    System.exit(1);
}
Properties appProps = new Properties();
try {
    appProps.load(appConfigStream);
} catch (IOException ex) {
    System.out.println("IO error during loading of {}...", appConfigLocation);
    System.exit(1);
}

read configs from ${basedir}. I like that instead of putting them to src/main/resources.

Check sources of ExecMojo.java for v1.2.1 http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.mojo/exec-maven-plugin/1.2.1/org/codehaus/mojo/exec/ExecMojo.java?av=f :

if ( CLASSPATH_TOKEN.equals( args[i] ) ) {
    commandArguments.add( computeClasspathString( null ) );
}

and and v1.3.2 http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.mojo/exec-maven-plugin/1.3.2/org/codehaus/mojo/exec/ExecMojo.java?av=f:

if ( args[i].contains( CLASSPATH_TOKEN ) ) {
     commandArguments.add( args[i].replace( CLASSPATH_TOKEN, 
                           computeClasspathString( null ) ) );
}

So update NB config Execute goals to new version:

process-classes org.codehaus.mojo:exec-maven-plugin:1.3.2:exec

and use complex -classpath args in exec.argsparams:

exec.args=-classpath %classpath:.:"${basedir}" \
    -Dconfig.location=app.properties \
    -Dlogback.configurationFile=logback.xml \
    ${packageClassName}

Fix any action you need to have such behavior:

enter image description here

See also:

  • http://wiki.netbeans.org/FaqEnvVarsDuringRun
  • http://wiki.netbeans.org/FaqSysPropsDuringRun
like image 177
gavenkoa Avatar answered Sep 29 '22 21:09

gavenkoa