英文:
How to add maven type solver in JavaParser
问题
我有一个使用Maven构建的AEM项目,依赖项由Maven注入。
我正在使用Java解析器库来对代码进行一些分析。
到目前为止,我们是通过手动注入类型解析器来使用m2存储库中JAR文件的路径,如下所示:
List<TypeSolver> typeSolvers = new ArrayList<TypeSolver>();
typeSolvers.add(new ReflectionTypeSolver());
typeSolvers.add(new JavaParserTypeSolver(new File(projectPath + "/src/main/java")));
for (String str : dependencies) {
typeSolvers.add(JarTypeSolver.getJarTypeSolver(str));
}
TypeSolver[] tsArray = new TypeSolver[typeSolvers.size()];
for (int i = 0; i < typeSolvers.size(); i++) {
tsArray[i] = typeSolvers.get(i);
}
typeSolver = new CombinedTypeSolver(tsArray);
我们是这样传递jar路径列表的:
List<String> dependencies = new ArrayList<>();
dependencies.add("C:/Users/"+Utility.USER_NAME+"/.m2/repository/com/adobe/aem/uber-jar/6.2.0/uber-jar-6.2.0-apis.jar");
现在,我希望类型解析器能够自动解析依赖项,而无需手动添加路径。
在Java中是否有类似于‘Maven类型解析器’的东西可以帮助实现相同的功能?
英文:
I have an AEM maven project with dependencies being injected by maven.
I am using the Java Parser library to do some analysis of the code.
Till now, we were manually injecting the typesolvers using the paths of the jars in the m2 repository like below
List<TypeSolver> typeSolvers = new ArrayList<TypeSolver>();
typeSolvers.add(new ReflectionTypeSolver());
typeSolvers.add(new JavaParserTypeSolver(new File(projectPath + "/src/main/java")));
for (String str : dependencies) {
typeSolvers.add(JarTypeSolver.getJarTypeSolver(str));
}
TypeSolver[] tsArray = new TypeSolver[typeSolvers.size()];
for (int i = 0; i < typeSolvers.size(); i++) {
tsArray[i] = typeSolvers.get(i);
}
typeSolver = new CombinedTypeSolver(tsArray);
We were passing it the list of jar paths like below:
List<String> dependencies = new ArrayList<>();
dependencies.add("C:/Users/"+Utility.USER_NAME+"/.m2/repository/com/adobe/aem/uber-jar/6.2.0/uber-jar-6.2.0-apis.jar");
Now, I would like the typesolver to automatically resolve the dependency without adding the paths manually.
Is there any 'Maven typesolver' like thing in Java which can help with the same.
答案1
得分: 0
看起来在Java解析器中直接实现这样的功能是不可能的。
我最终采取的方法是使用 org.apache.maven.model.io.xpp3.MavenXpp3Reader 解析我的 pom.xml 文件并获取依赖项/受控依赖项列表。
然后,我使用该列表生成jar包的路径(使用语法 <user.home>/.m2/repository/<group-id>/<artifact-id>/<version>/<artifact-id>-<version>-<classifier>.jar)。
然后将此jar文件路径传递给JarTypeSolver。
英文:
So looks like it is not possible to achieve something like this directly in Java parser.
What I ended up doing is to use org.apache.maven.model.io.xpp3.MavenXpp3Reader to parse my pom.xml file and get the list of dependencies/managed dependencies.
Then I used that list to generate the path of the jar's (using the syntax <user.home>/.m2/repository/<group-id>/<artifact-id>/<version>/<artifact-id>-<version>-<classifier>.jar).
This jar file path was then passed to JarTypeSolver.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论