代码实现 maven 私服jar包下载

201 阅读4分钟

通过代码在maven私服下载jar包有两种方式,一种是为通过URI链接直接下载,另一种是通过 # 利用aether api实现从指定maven仓库下载jar包

第一种http请求方式:

@Slf4j
@Data
public class HttpJarDownloader {

    private static final String CENTRAL_STORE = "http://artifactory.xx.com/libs-releases/";

    private static final String SNAPSHOTS = "http://artifactory.xx.com/libs-snapshots/";

    private static final String JAR_FILE_EXT_NAME = "-sources.jar";

    private static final String URL_SPLIT = "/";

    private static final String FILE_NAME_SPLIT = "-";

    /**
     * jar包下载目录
     *
     * @return
     */
    private static String getBasePath() {
        return "/Users/xxx/soft/jar";
    }

    /**
     * 检测远程jar是否存在
     *
     * @param location
     * @return
     */
    public static boolean checkExistedRemoteJar(MavenJarLocation location) {
        return readRemotePath(location);
    }


    /**
     * 检测本地jar是否存在
     *
     * @param location
     * @return
     */
    public static boolean checkExistedLocalJar(MavenJarLocation location) {
        return getMavenLocalFile(location) != null;
    }

    /**
     * 下载源码包优先查询本地、本地没有则查询远程
     *
     * @param location
     * @return
     */
    private static File downloadSourceJar(MavenJarLocation location) {
        File f = getMavenLocalFile(location);
        if (f != null) {
            log.info(">>>>>>>>>from local file>>>>>>>>" + f.getPath());
            log.info("download result form local file = {}", f.getPath());
            return f;
        }
        String filePath = downloadRemote(location);
        if (filePath != null) {
            return new File(filePath);
        }
        return null;
    }


    /**
     * 远程下载jar包或pom
     *
     * @return source jar下载到本地的路径
     */
    private static String downloadRemote(MavenJarLocation location) {
        String url = getMavenRemoteUrl(location);
        String fileName = getSourceJarFileName(location);
        log.info("center url : {}", url);
        String path = null;
        try {
            path = downLoadFromUrl(url, fileName, getBasePath());
        } catch (IOException e) {
            log.error("jar-pom location : {}, url : {}", location, url);
            log.error("maven download jar or pom file error", e);
        }
        return path;
    }

    /**
     * 获取本地 File
     *
     * @param location
     * @return
     */
    private static File getMavenLocalFile(MavenJarLocation location) {
        String url = getMavenLocalUrl(location);
        File f = new File(url);
        if (f.exists()) {
            if (!isSnapShot(location)) {
                return f;
            }
            //如果是大于1分钟则删除TODO 待定
            if (System.currentTimeMillis() - f.lastModified() > 1 * 60 * 1000) {
                f.delete();
            } else {
                return f;
            }
        }
        return null;
    }

    /**
     * 访问远程jar
     *
     * @param location
     * @return
     */
    private static boolean readRemotePath(MavenJarLocation location) {
        String url = getMavenRemoteUrl(location);
        log.info("center url : {}", url);
        Integer responseCode = null;
        try {
            responseCode = readFromUrl(url);
        } catch (IOException e) {
            log.error("readRemotePath location : {}, url : {}", location, url);
            log.error("readRemotePath error", e);
        }
        return Objects.equals(responseCode, 200);
    }


    /**
     * 获得maven远程url
     *
     * @param location
     * @return
     */
    private static String getMavenRemoteUrl(MavenJarLocation location) {
        String domain = CENTRAL_STORE;
        if (isSnapShot(location)) {
            domain = SNAPSHOTS;
        }
        return domain + getLocationPath(location) + getSourceJarFileName(location);
    }

    /**
     * 获得maven本地url+artifactId+version+fileName
     *
     * @param location
     * @return
     */
    private static String getMavenLocalUrl(MavenJarLocation location) {
        String domain = CENTRAL_STORE;
        if (isSnapShot(location)) {
            domain = SNAPSHOTS;
        }
        return getBasePath() + location.getArtifactId() + File.separator + location.getVersion() + File.separator + (getSourceJarFileName(location));
    }


    /**
     * maven私服上jar包路径
     *
     * @param location
     * @return
     */
    private static String getLocationPath(MavenJarLocation location) {
        return location.getGroupId().replace(".", "/") + URL_SPLIT + location.getArtifactId() + URL_SPLIT
                + location.getVersion() + URL_SPLIT;
    }


    /**
     * 拼接jar包文件名
     * 例:jcf-1.3.9.8-SNAPSHOT.sources.jar
     *
     * @param location
     * @return
     */
    private static String getSourceJarFileName(MavenJarLocation location) {
        return location.getArtifactId() + FILE_NAME_SPLIT + location.getVersion() + JAR_FILE_EXT_NAME;
    }

    /**
     * 是否是快照
     *
     * @param location
     * @return
     */
    private static boolean isSnapShot(MavenJarLocation location) {
        if (StringUtils.containsIgnoreCase(location.getVersion(), "SNAPSHOT")) {
            return true;
        }
        return false;
    }


    /**
     * 从网络Url中下载文件
     *
     * @param urlStr
     * @param fileName
     * @param savePath
     * @throws IOException
     */
    private static String downLoadFromUrl(String urlStr, String fileName, String savePath) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //设置超时间为3秒
        conn.setConnectTimeout(3 * 1000);
        //得到输入流
        InputStream inputStream = null;
        try {
            inputStream = conn.getInputStream();
        } catch (FileNotFoundException e) {
            log.error("file.err_download_file", e);
            throw StdException.adapt(e);
        }
        //获取自己数组
        byte[] getData = readInputStream(inputStream);
        //文件保存位置
        File saveDir = new File(savePath);
        if (!saveDir.exists()) {
            saveDir.mkdir();
        }
        File file = new File(saveDir + File.separator + fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(getData);
        if (fos != null) {
            fos.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
        return file.getPath();
    }

    /**
     * 判断网络Url中
     *
     * @param urlStr
     * @throws IOException
     */
    private static Integer readFromUrl(String urlStr) throws IOException {
        Integer responseCode = 0;
        //得到输入流
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3 * 1000);
            conn.setReadTimeout(3 * 1000);
            responseCode = conn.getResponseCode();
        } catch (FileNotFoundException e) {
            log.error("url is error >>>>>>>>>{}", urlStr);
            throw new IOException(e);
        }
        return responseCode;
    }


    /**
     * 从输入流中获取字节数组
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

    public static void main(String[] args) {
        try {
            MavenJarLocation location = new MavenJarLocation();
            location.setGroupId("com.jc");
            location.setArtifactId("qdox-explore-service");
            location.setVersion("1.0.0-SNAPSHOT");
            File file = downloadSourceJar(location);
            JarUtils.unzipJar(file, file.getParentFile());
            int i = 0;
        } catch (Exception e) {
            log.error("HttpJarDownloader.main Exception ",e);
        }
    }


}

利用aether api实现从指定maven仓库下载jar包

@Slf4j
public class AetherJarDownloader {

    /**
     *  create a repository system session
     * @param system RepositorySystem
     * @return RepositorySystemSession
     */
    private static RepositorySystemSession newSession( RepositorySystem system,String target )
    {
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
        session.setArtifactDescriptorPolicy( new SimpleArtifactDescriptorPolicy(true, true) );
        LocalRepository localRepo = new LocalRepository( /*"target/local-repo" */target);
        session.setLocalRepositoryManager( system.newLocalRepositoryManager( session, localRepo ) );
        return session;
    }
    /**
     * 建立RepositorySystem
     * @return RepositorySystem
     */
    private static RepositorySystem newRepositorySystem()
    {
        DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
        locator.addService( RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class );
        locator.addService( TransporterFactory.class, FileTransporterFactory.class );
        locator.addService( TransporterFactory.class, HttpTransporterFactory.class );
        return locator.getService( RepositorySystem.class );
    }
    public static List<File> downloadAllJar(MavenJarLocation location, File targetFile) throws  DependencyResolutionException, DependencyCollectionException {
        String groupId=location.getGroupId();
        String artifactId=location.getArtifactId();
        String version=location.getVersion();
        RepositorySystem repoSystem = newRepositorySystem();
        RepositorySystemSession session = newSession( repoSystem ,targetFile.getAbsolutePath());

        /**
         * 下载一个jar包
         */
        Artifact artifact=new DefaultArtifact(groupId+":"+artifactId+":"+version);
        Dependency dependency = new Dependency(artifact, null);
        CollectRequest collectRequest = new CollectRequest();
        collectRequest.setRoot(dependency);
        for (RemoteRepository remoteRepository : getxxxxRepository()) {
            collectRequest.addRepository(remoteRepository);
        }
        DependencyNode node = repoSystem.collectDependencies(session, collectRequest).getRoot();
        DependencyRequest dependencyRequest = new DependencyRequest();
        dependencyRequest.setRoot(node);
        dependencyRequest.setFilter(new DependencyFilter() {
            @Override
            public boolean accept(DependencyNode node, List<DependencyNode> parents) {
                log.info("开始下载依赖:{}",node.getArtifact().toString());
                return true;
            }
        });
        repoSystem.resolveDependencies(session, dependencyRequest);
        PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
        node.accept(nlg);
        return nlg.getFiles();
    }

    private static List<RemoteRepository> getxxxxRepository(){
        List<RemoteRepository>  list = new ArrayList<>();
        list.add((new RemoteRepository.Builder( "artifactory", "default", "https://artifactory.xxxx.com/libs-snapshots")
                .setSnapshotPolicy(new RepositoryPolicy(true,RepositoryPolicy.UPDATE_POLICY_ALWAYS,RepositoryPolicy.CHECKSUM_POLICY_WARN)).build()));
        list.add(new RemoteRepository.Builder( "snapshots", "default", "https://artifactory.xxxx.com/libs-releases" ).build());
        list.add(new RemoteRepository.Builder( "central", "default", "https://maven.aliyun.com/repository/central" ).build());
        {
            list.add(new RemoteRepository.Builder( "daojia", "default", "http://nexus.corp.imdada.cn/content/repositories/daojiaRepo/" ).build());
            list.add(new RemoteRepository.Builder( "dadaarchetype", "default", "http://nexus.corp.imdada.cn/content/repositories/public/" ).build());
            list.add(new RemoteRepository.Builder( "daddacentral", "default", "http://nexus.corp.imdada.cn/content/repositories/public/" ).build());
        }
        return list;
    }
    public static String getJarLatestVersion(MavenJarLocation location){
        File tempDir = Files.createTempDir();
        try{
            return getJarLatestVersion(location,tempDir);
        }finally {
            FileUtils.deleteQuietly(tempDir);
        }
    }
    public static String getJarLatestVersion(MavenJarLocation location, File targetFile){
        // 需要检查的jar包坐标
        String groupId=location.getGroupId();
        String artifactId=location.getArtifactId();
        String version=location.getVersion();
        // 创建Aether库相关的对象
        RepositorySystem repositorySystem = newRepositorySystem();
        RepositorySystemSession session =  newSession( repositorySystem ,targetFile.getAbsolutePath());;
        Artifact artifact=new DefaultArtifact(groupId+":"+artifactId+":"+version);
        ArtifactRequest artifactRequest=new ArtifactRequest();
        for (RemoteRepository remoteRepository : getxxxxRepository()) {
            artifactRequest.addRepository(remoteRepository);
        }
        artifactRequest.setArtifact(artifact);
        try {
            VersionRequest request = new VersionRequest();
            request.setArtifact(artifact);
            for (RemoteRepository remoteRepository : getxxxxRepository()) {
                request.addRepository(remoteRepository);
            }
            VersionResult result = repositorySystem.resolveVersion(session, request);
            return result.getVersion();
        }catch (Exception e){
            throw new BizException("获取jar包最新更新时间失败:"+e.getMessage(),e);
        }
    }
    public static boolean existMockJar(MavenJarLocation location,File targetFile){
        // 需要检查的jar包坐标
        String groupId=location.getGroupId();
        String artifactId=location.getArtifactId();
        String version=location.getVersion();

        // 仓库信息
        String repositoryUrl = "https://repo.maven.apache.org/maven2";
        String repositoryId = "central";
        // 创建Aether库相关的对象
        RepositorySystem repositorySystem = newRepositorySystem();
        RepositorySystemSession session =  newSession( repositorySystem ,targetFile.getAbsolutePath());;
        Artifact artifact=new DefaultArtifact(groupId+":"+artifactId+":"+version);
        ArtifactRequest artifactRequest=new ArtifactRequest();
        for (RemoteRepository remoteRepository : getxxxxRepository()) {
            artifactRequest.addRepository(remoteRepository);
        }
        artifactRequest.setArtifact(artifact);
        try {

            final ArtifactResult result = repositorySystem.resolveArtifact(session, artifactRequest);
            // 获取jar包文件路径
            File jarFile = result.getArtifact().getFile();
            if (jarFile.exists()) {
                log.info("artifact.success_resolve_maven_location:groupId={},artifactId={},version={}",groupId,artifactId,version);
                return true;
            } else {
                log.info("artifact.err_not_exist_maven_location:groupId={},artifactId={},version={}",groupId,artifactId,version);
                return false;
            }
        } catch (ArtifactResolutionException e) {
            log.error("artifact.err_resolve_maven_location:groupId={},artifactId={},version={}",groupId,artifactId,version,e);
            return false;
        }

    }
    public static File downloadSingleJar(MavenJarLocation location, File targetFile) throws ArtifactResolutionException
    {
        String groupId=location.getGroupId();
        String artifactId=location.getArtifactId();
        String version=location.getVersion();
        RepositorySystem repoSystem = newRepositorySystem();
        RepositorySystemSession session = newSession( repoSystem ,targetFile.getAbsolutePath());
        /**
         * 下载一个jar包
         */
        Artifact artifact=new DefaultArtifact(groupId+":"+artifactId+":"+version);
        ArtifactRequest artifactRequest=new ArtifactRequest();
        for (RemoteRepository remoteRepository : getxxxxRepository()) {
            artifactRequest.addRepository(remoteRepository);
        }
        artifactRequest.setArtifact(artifact);
        final ArtifactResult artifactResult = repoSystem.resolveArtifact(session, artifactRequest);
        return artifactResult.getArtifact().getFile();
    }
}