Ant未在Maven中运行的代码

时间:2012-04-28 11:35:28

标签: java r maven ant

这可能是我在Java中看到的最奇怪的事情:以下代码(不使用外部库)使用Ant(在Netbeans上,在Windows-7机器上)运行完美:

public static void main( String[] args ) throws IOException, InterruptedException
{
  String prog = "\"C:\\Program Files\\R\\R-2.14.2\\bin\\Rscript.exe\"";
  String[] progParams = new String[]{"C:\\Users\\pantelis\\Desktop\\SB\\MC_Sarfraz_ver5.R"};
  Process p = Runtime.getRuntime().exec(prog+" "+progParams[0]);
  p.waitFor();
  System.out.println(p.exitValue()==0);
  System.out.println("Done!!!"); 
}

并返回输出消息:

true
Done!!!

意味着它顺利完成。当我尝试使用Maven运行相同的代码时,我没有例外,但代码完成了:

false
Done!

并且永远不会执行该过程。这是我的POM文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany</groupId>
  <artifactId>MVNTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>MVNTest</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

更新1:我使用以下代码打印该流程生成的任何错误消息:

String line;
BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = input.readLine()) != null) {
  System.out.println(line);
}

这是输出:

Error in library(RDCOMClient) : there is no package called 'RDCOMClient'
Execution halted

仅当我使用Maven运行时才会显示此错误消息。

更新2:当我使用Maven运行时,我使用JDK7。这是相应的命令:

cd C:\Users\pantelis\Documents\NetBeansProjects\MVNTest; 
"JAVA_HOME=C:\\Program Files\\Java\\jdk1.7.0_04" 
"\"C:\\Program Files\\NetBeans 7.1.2\\java\\maven\\bin\\mvn.bat\""  
"-Dexec.args=-classpath %classpath com.mycompany.mvntest.App"
"-Dexec.executable=C:\\Program Files\\Java\\jdk1.7.0_04\\bin\\java.exe" 
-Dexec.classpathScope=runtime process-classes 
org.codehaus.mojo:exec-maven-plugin:1.2:exec

更新3:这些是我使用Ant运行时获得的环境变量:

USERPROFILE -> C:\Users\pantelis
ProgramData -> C:\ProgramData
PATHEXT -> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Classpath -> C:\LINGO9\Lingo9_0.jar
ProgramFiles(x86) -> C:\Program Files (x86)
TEMP -> C:\Users\pantelis\AppData\Local\Temp
SystemDrive -> C:
ProgramFiles -> C:\Program Files
Path -> C:\Program Files (x86)\MiKTeX 2.9\miktex\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;\;C:\Program Files (x86)\Sony\VAIO Startup Setting Tool;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\LINGO9\;C:\Program Files (x86)\OpenVPN\bin
HOMEDRIVE -> C:
PROCESSOR_REVISION -> 2505
USERDOMAIN -> pantelis-VAIO
ALLUSERSPROFILE -> C:\ProgramData
ProgramW6432 -> C:\Program Files
PROCESSOR_IDENTIFIER -> Intel64 Family 6 Model 37 Stepping 5, GenuineIntel
SESSIONNAME -> Console
TMP -> C:\Users\pantelis\AppData\Local\Temp
CommonProgramFiles -> C:\Program Files\Common Files
=:: -> ::\
LOGONSERVER -> \\PANTELIS-VAIO
PROCESSOR_ARCHITECTURE -> AMD64
FP_NO_HOST_CHECK -> NO
OS -> Windows_NT
HOMEPATH -> \Users\pantelis
PROCESSOR_LEVEL -> 6
CommonProgramW6432 -> C:\Program Files\Common Files
configsetroot -> C:\Windows\ConfigSetRoot
LOCALAPPDATA -> C:\Users\pantelis\AppData\Local
COMPUTERNAME -> PANTELIS-VAIO
windir -> C:\Windows
SystemRoot -> C:\Windows
NUMBER_OF_PROCESSORS -> 4
USERNAME -> pantelis
PUBLIC -> C:\Users\Public
PSModulePath -> C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
CommonProgramFiles(x86) -> C:\Program Files (x86)\Common Files
ComSpec -> C:\Windows\system32\cmd.exe
APPDATA -> C:\Users\pantelis\AppData\Roaming

以下是我使用Maven运行时的环境变量:

USERPROFILE -> C:\Users\pantelis
JAVA_HOME -> C:\Program Files\Java\jdk1.7.0_04
PROGRAMDATA -> C:\ProgramData
MAVEN_CMD_LINE_ARGS -> "-Dexec.args=-classpath %classpath com.mycompany.mvntest.App" "-Dexec.executable=C:\Program Files\Java\jdk1.7.0_04\bin\java.exe" -Dexec.classpathScope=runtime process-classes org.codehaus.mojo:exec-maven-plugin:1.2:exec
CONFIGSETROOT -> C:\Windows\ConfigSetRoot
COMMONPROGRAMFILES -> C:\Program Files\Common Files
PROCESSOR_REVISION -> 2505
USERDOMAIN -> pantelis-VAIO
ALLUSERSPROFILE -> C:\ProgramData
PROGRAMW6432 -> C:\Program Files
SESSIONNAME -> Console
TMP -> C:\Users\pantelis\AppData\Local\Temp
PSMODULEPATH -> C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
NB_EXEC_MAVEN_PROCESS_UUID -> b5daa24f-51dc-48ac-ad6d-820dc245b80a
M2_HOME -> C:\Program Files\NetBeans 7.1.2\java\maven\bin\..
LOGONSERVER -> \\PANTELIS-VAIO
=:: -> ::\
PATH -> C:\Program Files (x86)\MiKTeX 2.9\miktex\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64;\;C:\Program Files (x86)\Sony\VAIO Startup Setting Tool;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\LINGO9\;C:\Program Files (x86)\OpenVPN\bin
PROMPT -> $P$G
COMMONPROGRAMW6432 -> C:\Program Files\Common Files
PROCESSOR_LEVEL -> 6
LOCALAPPDATA -> C:\Users\pantelis\AppData\Local
COMPUTERNAME -> PANTELIS-VAIO
CLASSWORLDS_LAUNCHER -> org.codehaus.plexus.classworlds.launcher.Launcher
USERNAME -> pantelis
APPDATA -> C:\Users\pantelis\AppData\Roaming
WINDIR -> C:\Windows
PATHEXT -> .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROGRAMFILES(X86) -> C:\Program Files (x86)
TEMP -> C:\Users\pantelis\AppData\Local\Temp
HOMEDRIVE -> C:
SYSTEMDRIVE -> C:
=C: -> C:\Users\pantelis\Documents\NetBeansProjects\MVNTest
ERROR_CODE -> 0
COMMONPROGRAMFILES(X86) -> C:\Program Files (x86)\Common Files
CLASSWORLDS_JAR -> "C:\Program Files\NetBeans 7.1.2\java\maven\bin\..\boot\plexus-classworlds-2.4.jar"
PROCESSOR_IDENTIFIER -> Intel64 Family 6 Model 37 Stepping 5, GenuineIntel
MAVEN_JAVA_EXE -> "C:\Program Files\Java\jdk1.7.0_04\bin\java.exe"
CLASSPATH -> C:\LINGO9\Lingo9_0.jar
PROCESSOR_ARCHITECTURE -> AMD64
FP_NO_HOST_CHECK -> NO
OS -> Windows_NT
HOMEPATH -> \Users\pantelis
COMSPEC -> C:\Windows\system32\cmd.exe
PROGRAMFILES -> C:\Program Files
HOME -> C:\Users\pantelis
NUMBER_OF_PROCESSORS -> 4
PUBLIC -> C:\Users\Public
SYSTEMROOT -> C:\Windows

3 个答案:

答案 0 :(得分:1)

更新后,基于从maven运行时得到的错误,似乎某些(类)路径未正确设置:RDCOMClient似乎与R相关,因此它是用于运行Rscript的设置.exe就是问题。

在通过ant运行时以及在maven下运行环境变量并进行比较以确定缺少库错误的来源

评论更新:

问题可能与如何从maven执行项目有关:exec:exec启动一个新的JVM,而exec:java在运行maven的同一个JVM中运行项目。

评论更新:

显然,NetBeans中的“保存时编译”设置会将maven目标从exec:exec更改为exec:java。或者,您可以手动配置pom.xml(请参阅exec maven plugin页面,其中包含两个目标的示例和说明)

答案 1 :(得分:1)

所有R套餐都会发生这种情况吗?或者只有RDCOMClient?

如果这是一个Maven / Ant问题,我会非常惊讶。

不需要读取错误流。 R在文件中输出错误,或者您可以使用sink(“filename”)将R输出写入文件。

请视为评论。

答案 2 :(得分:0)

我认为,您需要在数组中指定prog和params,并将该数组传递给exec。

public static void main( String[] args ) throws IOException, InterruptedException
{
  String prog = "\"C:\\Program Files\\R\\R-2.14.2\\bin\\Rscript.exe\"";
  String[] progParams = new String[] {
     prog,
     "C:\\Users\\pantelis\\Desktop\\SB\\MC_Sarfraz_ver5.R"
  };
  Process p = Runtime.getRuntime().exec(progParams);
  p.waitFor();
  System.out.println(p.exitValue()==0);
  System.out.println("Done!!!"); 
}