java - 使用 maven 依赖项从 ant 运行项目 java 类

标签 java ant maven dependencies classpath

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

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

这就是我要做的;创建一个路径(运行),并使用 Maven ant 任务从 Maven 添加依赖项,然后运行一个调用 Java 类 (MyClass) 的目标,该类已编译为目标\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. 一种替代方法来完成我正在尝试做的事情(描述 以上)?

最佳答案

再玩一会儿给了我一个可行的解决方案...

我可以使用文件集来引用它们,而不是将 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>

我不知道其他方法是怎么回事,是用户错误还是错误,所以如果有人有任何建议,我将不胜感激。

关于java - 使用 maven 依赖项从 ant 运行项目 java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7204046/

相关文章:

ant - 使用ivy在ant中构建 "fat jar",而不将jar复制到lib目录

maven - mvn 测试与 mvn surefire :test

java - Hadoop 2.5.2 mvn 构建失败 Windows 7 x64 - hadoop-common 上的 compile-ms-native-dll?

java - 为什么 <value>!0</value> 匹配默认值 0?

java - 更改 TableCellEditor 对象的对齐方式

java - postgres 和 java 的内存缓存选项

java - 仅在编译时设置java类路径

Ant - Java 命令 - IllegalAccessException

java - Teradata 多个连接 INSERT 死锁

Java 8 Streams : Make Collectors. groupingBy 返回 Map<K, List<V>> 而不是 Map<K, List<List<V>>>