maven手工批量上传本地maven三方jar包

本文介绍了一种方法,通过编写Java脚本解决在Maven仓库手动上传包含snapshot包的第三方jar文件的问题,脚本包括读取jar和pom文件信息,生成并写入部署命令到文本文件中。

最近因为三方代码管理问题,需要手工上传三方的包到maven仓库。但snapshot包是不能通过界面上传的,只能编写脚本。可以那么多jar,一个个写脚本,脚本就得写很多年了。代码整起。

public class UploadPomJar {
    // maven用户密码配置
    private static final String settingPath = "D:\\maven-setting.xml";

    public static void main(String[] args) throws Exception {
        String rootPath = "C:\\Users\\34657\\Desktop\\mavenjarrootpath";
        // 把上传命令生成到文本内,执行粘贴就行
        Path path = Paths.get("C:\\Users\\34657\\Desktop\\pushJar.txt");
        File file = new File(rootPath);
        List<File> leafFiles = new ArrayList<>();
        getLeafFiles(file, leafFiles);
        for (File leaf : leafFiles) {
            uploadFileJar(path, leaf);
        }
    }
    //将单个jar写入文本一行
    private static void uploadFileJar(Path path, File leaf) throws Exception {
        String fileName = leaf.getName();
        String repository = "public";
        if (fileName.endsWith("SNAPSHOT")) {
            repository = "snapshots";
        }
        File jarFile = getJarFile(leaf);
        if (null == jarFile) {
            return;
        }
        String fileFullPath = jarFile.getAbsolutePath();
        File pomFile = new File(fileFullPath.replace("jar", "pom"));
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(pomFile);
        Element rootElement = document.getRootElement();
        String groupId = rootElement.elementText("groupId");
        String artifactId = rootElement.elementText("artifactId");
        String version = rootElement.elementText("version");
        String pushCmd = getPushCmd(repository, fileFullPath, groupId, artifactId, version);
        Files.write(path, pushCmd.concat("\n").getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
    }
    //获取jar文件,jar文件需要排除snapshot临时版本文件
    private static File getJarFile(File leaf) {
        for (File f : leaf.listFiles()) {
            if (!f.getName().endsWith(".jar")) {
                continue;
            }
            //jar文件需要排除snapshot临时版本文件
            if (f.getName().endsWith(leaf.getName() + ".jar")) {
                return f;
            }
        }
        return null;
    }

    //递归获取叶子节点
    private static void getLeafFiles(File file, List<File> leafFiles) {
        if (isLeafDir(file)) {
            leafFiles.add(file);
            return;
        }
        for (File f : file.listFiles()) {
            getLeafFiles(f, leafFiles);
        }
    }

    //判断jar所在文件夹
    private static boolean isLeafDir(File file) {
        return file.listFiles(File::isDirectory).length == 0;
    }

    private static String getPushCmd(String repository, String fileFullPath, String groupId, String artifactId, String version) {
        return "mvn deploy:deploy-file"
                + " -DgroupId=" + groupId
                + " -DartifactId=" + artifactId
                + " -Dversion=" + version
                + " -Dpackaging=jar "
                + " -Dfile=" + fileFullPath
                + " -DpomFile=" + fileFullPath.replace(".jar",".pom")
//                + " -DgeneratePom=true"
                + " -DrepositoryId=maven-" + repository
                + " -Durl=https://maven.com/repository/pubrepo/maven-" + repository + "/"
                + " --settings " + settingPath;
    }
}
使用 Maven 将项目打JAR 并提供给第三方,有以下两种常见情况及对应方法: ### 打项目自带依赖的情况 若要将 Maven 项目的一些文件打JAR 提供给第三方,可使用 maven assembly 插件。在 `pom.xml` 里添加如下配置: ```xml <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <skipAssembly>${skipAssembly}</skipAssembly> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> ``` 配置完成后,执行相应的 Maven 命令,就会在打目录的 `target` 文件夹下生成 JAR ,可将其提供给第三方使用[^3]。 ### 引入第三方 JAR 并打的情况 当项目需要引入第三方 JAR 并打时,操作步骤如下: 1. 选择 `File` 下的 `project Structure`。 2. 选择 `Module`,选择项目模块,在 `Dependencies` 下点击加号,再点击 `JARs or Directories…`。 3. 选择 JAR 所在位置,点击 `OK`,再点击 `Apply`,此时 JAR 已添加到本地环境。 4. 打时要配置 `pom` 文件,引入第三方的 `artifactId`,添加 `scope` 标签且值为 `system`,同时添加 `systemPath`,其值为本地 JAR 所在路径。 5. 在 `spring-boot-maven-plugin` 下配置 `includeSystemScope` 为 `true`。配置后打时会把外部引入的 JAR 到项目 JAR 中,确保项目在服务器上能正常运行;若不配置,本地能运行,但服务器上的 JAR 中没有该外部。 6. 执行 `mvn clean install` 或者 `mvn clean package`。`maven` 的 `install` 会把打结果放到本地 `maven` 仓库,其他模块可调用;`package` 则不会。打成功后,会在打目录的 `target` 文件夹下生成 JAR ,将其提供给第三方即可[^4]。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值