对于我们的项目,我们需要在运行时编译几个类文件,我们已经开始使用JavaCompiler
类。在Eclipse中一切正常,但是当我们尝试构建(Maven)和deploy
时,我们收到“package does not exist
”错误。
代码优先:
Files.walk(Paths.get(inputTestcaseFolder))
.filter(Files::isRegularFile)
.filter(path -> path.toAbsolutePath().toString().contains(".java"))
.forEach(fileList::add);
for (Path p : fileList) {
System.out.println("Test Compiled : " + p.getFileName().toString());
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, p.toString());
}
还尝试引用类路径仍然是同一个问题
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
List<String> files = new ArrayList<>();
files.add(p.toString());
Iterable<? extends JavaFileObject> compilationUnits1 =
fileManager.getJavaFileObjectsFromStrings(files);
optionList.addAll(Arrays.asList("-classpath",System.getProperty("java.class.path")));
compiler.getTask(null, fileManager, null, optionList, null, compilationUnits1).call();
fileManager.close();
Jenkins工作配置:
构建目标和选项:
-e -X clean compile dependency:copy-dependencies exec:java
-Dexec.mainClass="Performance.BaseFixture.Base" -DM2=D:\ -DM2_HOME=C:\apache-maven-3.2.3
-Dmaven.repo.local=D:\ -Dexec.includeProjectDependencies=true
错误:
[DEBUG] Adding project dependency artifact: httpclient to classpath
[DEBUG] Adding project dependency artifact: xmlbeans to classpath
[DEBUG] Adding project dependency artifact: stax-api to classpath
[DEBUG] Adding project dependency artifact: zip4j to classpath
[DEBUG] joining on thread Thread[Performance.BaseFixture.Base.main(),5,Performance.BaseFixture.Base]
[DEBUG] Setting accessibility to true in order to invoke main().
success
Test Compiled : CaseListFlow.java
src\test\java\TestCases\CaseListFlow.java:3: error: package HTTPClient does not exist
import HTTPClient.HTTPResponse;
^
src\test\java\TestCases\CaseListFlow.java:4: error: package HTTPClient does not exist
import HTTPClient.NVPair;
^