无需直接执行SVN调用,Maven获取SCM信息及Jenkins插件改造咨询
我来帮你拆解这两个问题,一步步给你靠谱的解决方案:
一、不直接执行SVN调用从Maven中获取SCM信息
其实不用依赖外部svn命令行工具,Maven的SCM插件支持用纯Java的SVNKit库来和SVN仓库交互,全程不需要调用外部可执行文件。具体操作可以按下面的步骤来:
第一步:在
pom.xml中配置maven-scm-plugin,指定使用SVNKit作为SCM provider,同时启用扩展加载SVNKit依赖:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-scm-plugin</artifactId> <version>1.11.2</version> <!-- 适配Maven3.5.3的稳定版本 --> <dependencies> <dependency> <groupId>org.apache.maven.scm</groupId> <artifactId>maven-scm-provider-svnkit</artifactId> <version>1.11.2</version> </dependency> </dependencies> <configuration> <providerImplementations> <svn>svnkit</svn> </providerImplementations> </configuration> <extensions>true</extensions> </plugin> </plugins> </build>第二步:确保
pom.xml的<scm>节点配置正确,指向你的SVN仓库:<scm> <connection>scm:svn:https://your-svn-repo-url/path/to/project</connection> <developerConnection>scm:svn:https://your-svn-repo-url/path/to/project</developerConnection> <url>https://your-svn-repo-url/browse/path/to/project</url> </scm>第三步:获取并写入SCM信息到
META-INF:- 可以用
maven-buildnumber-plugin结合SVNKit拿取版本号等信息,配置如下:<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.4</version> <executions> <execution> <phase>validate</phase> <goals> <goal>create</goal> </goals> </execution> </executions> <configuration> <scmProviderImplementations> <svn>svnkit</svn> </scmProviderImplementations> <revisionOnScmFailure>UNKNOWN</revisionOnScmFailure> </configuration> <dependencies> <dependency> <groupId>org.apache.maven.scm</groupId> <artifactId>maven-scm-provider-svnkit</artifactId> <version>1.11.2</version> </dependency> </dependencies> </plugin> - 在
src/main/resources/META-INF下创建模板文件(比如scm-info.properties):scm.url=${project.scm.connection} scm.revision=${buildNumber} scm.branch=${scmBranch} - 用
maven-resources-plugin替换模板中的属性并输出到构建目录的META-INF:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <executions> <execution> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.outputDirectory}/META-INF</outputDirectory> <resources> <resource> <directory>src/main/resources/META-INF</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
执行
mvn clean install后,就能在构建产物的META-INF里看到填充好的SCM信息文件了。- 可以用
二、Jenkins环境下改造插件(无外部SVN路径配置)
针对Jenkins环境未配置SVN路径的情况,核心思路是利用内置的SVNKit支持,避免依赖外部可执行文件,有两种可行方案:
方案一:用现有Jenkins插件结合Maven流程
- 确保Jenkins安装了Subversion插件(选兼容Java1.8的稳定版,比如2.15.0),该插件默认内置SVNKit,不需要在Jenkins节点配置外部SVN路径。
- 在Jenkins构建任务中直接调用前面配置好的Maven目标(
mvn clean install),Maven会通过SVNKit自动获取SCM信息并写入META-INF,全程无需外部svn命令。
方案二:改造自定义Jenkins插件
如果是自己开发或改造Jenkins插件,直接在代码中集成SVNKit API来获取SCM信息:- 在插件的
pom.xml中引入兼容Java1.8的SVNKit依赖:<dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> <version>1.10.11</version> </dependency> - 示例代码片段(获取当前工作目录的SVN信息):
import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.wc.SVNClientManager; import org.tmatesoft.svn.core.wc.SVNInfo; import org.tmatesoft.svn.core.wc.SVNWCUtil; import java.io.File; public class SvnInfoFetcher { public static SVNInfo getSvnInfo(String workingDir) throws SVNException { SVNClientManager clientManager = SVNClientManager.newInstance(); return clientManager.getWCClient().doInfo(new File(workingDir), SVNRevision.WORKING); } } - 获取到
SVNInfo后,提取版本号、仓库URL等信息,写入到项目的META-INF文件夹即可。
- 在插件的
内容的提问来源于stack exchange,提问作者ScrappyDev
相关产品推荐
相关产品推荐

