Sonar实践问题:指定第三方库目录
2020.03.18 18:50:22字数 456阅读 1,022
理论已经由别人提出
added the target “dependency:copy-dependencies” as part of the maven execution. This copied all the dependencies to the right location, then I set the property “-Dsonar.java.libraries=target/dependency” in our case.
我的实践如下——
这个插件的官方maven-dependency-plugin 使用文档,根据样例如下——
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
这样有两个问题:
- 在多模块下自定义的这个outputDirectory目录是相对目录,会将依赖的jar包复制到子模块对应的目录下;
- 根据这里的解决方案使用变量
${session.executionRootDirectory}/target/
,可以强制将依赖包都复制到固定目录,但只能在执行mvn package
阶段才生效
这样导致执行了接近全生命周期的任务。
这里涉及到phase
、goal
的概念区别:What are Maven goals and phases and what is their difference?
Life cycle is a sequence of named phases.
Phases executes sequentially. Executing a phase means executes all previous phases.
Plugin is a collection of goals also called MOJO (Maven Old Java Object).
Analogy : Plugin is a class and goals are methods within the class.
default Maven lifecycle bindings这里定义了在各个“阶段”(phase)都会执行哪些“目标动作”(goals)
mvn [plugin-name]:[goal-name]
,例如mvn dependency:copy-dependencies
就是执行了插件maven-dependency-plugin
定义的copy-dependencies
动作。进一步,对应的configuration可以通过变量进行传参。
经验证,使用绝对路径地址指定outputDirectory的值是,可以将多模块的依赖统一复制到指定目录下。
所以,最终的解决方案为:
- 直接执行goal,
mvn dependency:copy-dependencies -DoutputDirectory=absolute/path/to/lib/dir
将所有的依赖复制到指定目录, - 将上面的目录传递给sonar的
sonar.java.libraries
变量,例如-Dsonar.java.libraries=absolute/path/to/lib/dir/*.jar