开发者

Running project java class from ant using maven for dependencies

开发者 https://www.devze.com 2023-03-31 04:06 出处:网络
I am trying to run a Java class as part of the deployment of my project (I want to create some resources at deployment, which can then be read at runtime).

I am trying to run a Java class as part of the deployment of my project (I want to create some resources at deployment, which can then be read at runtime).

For the most part, I am using maven for the build cycle - in particular for dependency management.

This what I've got to; creating a path (run), and adding the dependencies from maven using the maven ant tasks, and then running a target that calls a java class (MyClass), which has been compiled to ...MyClass.class in the target\src directory, using a classpath of that directory and the run path specified above.

<path id="run" />
<artifact:dependencies pathid="run">
  <artifact:pom file="pom.xml" id="my_project" />
</artifact:dependencies>

<target name="runMyClass">
  <java classname="...MyClass" fork="yes" maxmemory="512M" append="true">
    <classpath>
      <pathelement location="target\classes"/>
      <pathelement id="run" />
    </classpath>
  </java>
</target>

I know that the target\classes is correct - if I comment out the addition of the run path, it finds the class, but reports that some of the imports in the class are not available on the classpath.

However, when I run this, I get the following stack trace:

C:\somepath\my_project\build.xml:118: java.lang.NullPointerException
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:116)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:390)
at org.apache.tools.ant.Target.performTasks(Target.java:411)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:809)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.ant.execution.AntMain2.main(AntMain2.java:32)
Caused by: java.lang.NullPointerException
at org.apache.tools.ant.types.resources.FileResourceIterator.addFiles(FileResourceIterator.java:104)
at org.apache.tools.ant.types.resources.FileResourceIterator.<init>(FileResourceIterator.java:95)
at org.apache.tools.ant.types.Path$PathElement.iterator(Path.java:124)
at org.apache.tools.ant.types.resources.Union.getCollection(Union.java:123)
at org.apache.tools.ant.types.resources.Union.getCollection(Union.java:107)
at org.apache.tools.ant.types.resources.BaseResourceCollectionContainer.cacheCollection(BaseResourceCollectionContainer.java:265)
at org.apache.tools.ant.types.resources.BaseResourceCollectionContainer.iterator(BaseResourceCollectionContainer.java:142)
at org.apache.tools.ant.types.Path.iterator(Path.java:710)
at org.apache.tools.ant.types.resources.Union.getCollection(Union.java:123)
at org.apache.tools.ant.types.resources.Union.list(Union.java:86)
at org.apache.tools.ant.types.Path.list(Path.java:378)
at org.apache.tools.ant.types.Path.addExisting(Path.java:331)
at org.apache.tools.ant.types.Path.addExisting(Path.java:319)
at org.apache.tools.ant.types.Path.concatSpecialPath(Path.java:572)
at org.apache.tools.ant.types.Path.concatSystemClasspath(Path.java:532)
at org.apache.tools.ant.types.CommandlineJava.haveClasspath(CommandlineJava.java:647)
at org.apache.tools.ant.types.CommandlineJava.addCommandsToList(CommandlineJava.java:437)
at org.apache.tools.ant.types.CommandlineJava.getCommandline(CommandlineJava.java:405)
at org.apache.tools.ant.types.CommandlineJava.describeCommand(CommandlineJava.java:482)
at org.apache.tools.ant.taskdefs.Java.checkConfiguration(Java.java:176)
at org.apache.tools.ant.taskdefs.Java.execute(Java.java:107)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.a开发者_如何学编程nt.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
... 16 more

To me, this looks like an exception is being thrown in the ant code in the process of adding the path set to the classpath, but I could be wrong.

Can anyone suggest (any of the following):

  1. how I might go about debugging this?

  2. an alternative approach to do what I'm trying to do (described above)?


A little further playing gave me a workable solution...

Rather than refering to the maven dependencies as a path, I can refer to them using a fileset:

<fileset id="run" />
<artifact:dependencies filesetid="run">
  <artifact:pom file="pom.xml" id="my_project" />
</artifact:dependencies>  

<target name="runMyClass">
  <java classname="...MyClass" fork="yes" maxmemory="512M" append="true">
    <classpath>
      <pathelement location="target\classes"/>
      <fileset refid="run" />
    </classpath>
  </java>
</target>

I don't know what was going on with the other approach, whether it's user error or a bug, so if anyone has any suggestions, I would appreciate comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消