如何在JavaParser中添加Maven类型求解器

huangapple go评论64阅读模式
英文:

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&lt;TypeSolver&gt; typeSolvers = new ArrayList&lt;TypeSolver&gt;();
	typeSolvers.add(new ReflectionTypeSolver());
	typeSolvers.add(new JavaParserTypeSolver(new File(projectPath + &quot;/src/main/java&quot;)));
	for (String str : dependencies) {
		typeSolvers.add(JarTypeSolver.getJarTypeSolver(str));
	}
	TypeSolver[] tsArray = new TypeSolver[typeSolvers.size()];
	for (int i = 0; i &lt; typeSolvers.size(); i++) {
		tsArray[i] = typeSolvers.get(i);
	}
	typeSolver = new CombinedTypeSolver(tsArray);

We were passing it the list of jar paths like below:

    List&lt;String&gt; dependencies = new ArrayList&lt;&gt;();
	dependencies.add(&quot;C:/Users/&quot;+Utility.USER_NAME+&quot;/.m2/repository/com/adobe/aem/uber-jar/6.2.0/uber-jar-6.2.0-apis.jar&quot;);

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/&lt;group-id>/&lt;artifact-id>/&lt;version>/&lt;artifact-id>-&lt;version>-&lt;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/&lt;group-id>/&lt;artifact-id>/&lt;version>/&lt;artifact-id>-&lt;version>-&lt;classifier>.jar).

This jar file path was then passed to JarTypeSolver.

huangapple
  • 本文由 发表于 2023年2月24日 14:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553216.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定