【问题标题】:Running project java class from ant using maven for dependencies使用 maven 从 ant 运行项目 java 类以获取依赖项
【发布时间】:2011-11-04 11:10:42
【问题描述】:

我正在尝试运行一个 Java 类作为我项目部署的一部分(我想在部署时创建一些资源,然后可以在运行时读取)。

在大多数情况下,我将 maven 用于构建周期 - 特别是用于依赖管理。

这是我必须要做的;创建路径(运行),并使用 maven ant 任务添加来自 maven 的依赖项,然后运行调用 java 类(MyClass)的目标,该目标已编译为 target\src 目录中的 ...MyClass.class ,使用该目录的类路径和上面指定的运行路径。

<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>

我知道 target\classes 是正确的 - 如果我注释掉添加的运行路径,它会找到类,但报告类中的某些导入在类路径上不可用。

但是,当我运行它时,我得到以下堆栈跟踪:

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.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
... 16 more

对我来说,这看起来像是在将路径集添加到类路径的过程中在 ant 代码中引发了异常,但我可能是错的。

任何人都可以建议(以下任何一项):

  1. 我该如何调试这个?

  2. 另一种方法来做我想做的事(描述 以上)?

【问题讨论】:

    标签: java ant maven dependencies classpath


    【解决方案1】:

    多玩一点给了我一个可行的解决方案...

    我可以使用文件集来引用它们,而不是将 maven 依赖项称为路径:

    <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>
    

    我不知道其他方法发生了什么,无论是用户错误还是错误,所以如果有人有任何建议,我将不胜感激 cmets。

    【讨论】:

      猜你喜欢
      • 2014-05-25
      • 2014-08-10
      • 2016-08-15
      • 2011-10-29
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2012-03-30
      • 2016-03-08
      相关资源
      最近更新 更多