`
teclick
  • 浏览: 3136 次
社区版块
存档分类
最新评论

打造适合自己使用的Maven私服

阅读更多

Artifactory 是一个非常好用的私服产品,有两个分支:
    一个是开源的OOS(From version 4.8.1, Artifactory OSS is licensed under AGPL 3.0 (previously LGPL 3.0))
    一个是需要付费的PRO,10几万,贵的有点离谱,但功能也更丰富。
这里主要是改造开源的那个版本,你可以从他们的网站下载到源代码,然后自己编译调试。
文件的属性,只有PRO版本才会有,但是我们自己写点代码这个可以有。还有就是Rest API,也是只有PRO才会提供,但是我们也可以在不改变原有程序的情况下,添加一个我们自己写的一个类,实现我们需要的API,这个也可以有。

我这里以3.5.3版本为例,描述下:
添加属性的方法,按照下面方法,即可以把属性显示出来 package org.artifactory.addon.wicket;

    private static class DisabledPropertiesPanel extends PropertiesPanel {
        public DisabledPropertiesPanel(String id, String nestedPanelId) {
            super(id);
            //add(new DisabledCollapsibleBehavior()); <-- 注释掉这行即可以把属性展示出来       
            add(new WebMarkupContainer(nestedPanelId));
        }

        @Override
        protected Component newToolbar(String id) {
            return new DisabledAddonHelpBubble(id, PROPERTIES);
        }
    }

 

添加自定义的API的方式更简单,只需把我们实现的API放到 package org.artifactory.rest.resource.* 下,我的是放到 package org.artifactory.rest.resource.versions; 下了。

package org.artifactory.rest.resource.versions;

import org.artifactory.api.config.CentralConfigService;
import org.artifactory.api.module.ModuleInfo;
import org.artifactory.api.repo.RepositoryService;
import org.artifactory.api.security.AuthorizationService;
import org.artifactory.fs.ItemInfo;
import org.artifactory.fs.StatsInfo;
import org.artifactory.md.Properties;
import org.artifactory.model.common.RepoPathImpl;
import org.artifactory.model.xstream.fs.PropertiesImpl;
import org.artifactory.repo.RepoPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.*;

/**
 * Created by Nelson on 2016-12-06.
 * VersionMaintenanceResources
 */
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Path(VersionMaintenanceResources.PATH_ROOT)
@RolesAllowed({AuthorizationService.ROLE_USER, AuthorizationService.ROLE_ADMIN})
public class VersionMaintenanceResources {

    private static final Logger logger = LoggerFactory.getLogger(VersionMaintenanceResources.class);

    public static final String PATH_ROOT = "version-maintenance";

    @Context
    private HttpServletRequest request;

    @Autowired
    private RepositoryService repoService;

    @Autowired
    private CentralConfigService centralConfig;

    @Autowired
    private AuthorizationService authorizationService;

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    @Path("property/get/{repoKey: (?!_any)[^/]+}/{path: (?!_any).+}")
    public Response getRepoPropertiesByPath(@PathParam("repoKey") String repoKey, @PathParam("path") String path) {
        Response response = hasPermission();
        if (null != response) {
            return response;
        }
        RepoPath repoPath = new RepoPathImpl(repoKey, path);
        if (repoService.exists(repoPath)) {
            Properties properties = repoService.getProperties(repoPath);
            Map<String, String> result = new HashMap<>();
            if (null != properties) {
                for (String key : properties.keySet()) {
                    Set<String> strings = properties.get(key);
                    if (null != strings) {
                        result.put(key, strings.toString());
                    }
                }
            }

            return Response.ok(result, MediaType.APPLICATION_JSON_TYPE).build();
        }

        return Response.status(Response.Status.BAD_REQUEST)
                .entity("Repo path not exists")
                .type(MediaType.APPLICATION_JSON_TYPE)
                .build();
    }

    @PUT
    @Produces({MediaType.APPLICATION_JSON})
    @Path("property/set/{repoKey: (?!_any)[^/]+}/{path: (?!_any).+}")
    public Response setRepoPropertiesByPath(@PathParam("repoKey") String repoKey, @PathParam("path") String path) {
        Response response = hasPermission();
        if (null != response) {
            return response;
        }
        RepoPath repoPath = new RepoPathImpl(repoKey, path);
        if (repoService.exists(repoPath)) {
            Properties properties = repoService.getProperties(repoPath);
            if (null == properties) {
                properties = new PropertiesImpl();
            }
            Map<String, String[]> parameterMap = request.getParameterMap();
            if ((null == parameterMap) || parameterMap.isEmpty()) {
                properties.clear();
            }

            if (null != parameterMap) {
                for (String propName : parameterMap.keySet()) {
                    if (properties.containsKey(propName)) {
                        properties.removeAll(propName);
                    }
                    String[] values = parameterMap.get(propName);
                    if (null != values) {
                        for (String value : values) {
                            if ((null != value) && (value.trim().length() > 0)) {
                                properties.put(propName, value);
                            }
                        }
                    }
                }

                repoService.setProperties(repoPath, properties);

                return Response.ok("Success", MediaType.APPLICATION_JSON_TYPE).build();
            }
        }

        return Response.status(Response.Status.BAD_REQUEST)
                .entity("Repo path not exists")
                .type(MediaType.APPLICATION_JSON_TYPE)
                .build();
    }

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    @Path("list/{repoKey: (?!_any)[^/]+}/{path: (?!_any).+|$}")
    public Response getChildRepoByPath(@PathParam("repoKey") String repoKey, @PathParam("path") String path) {
        Response response = hasPermission();
        if (null != response) {
            return response;
        }

        List<FileInfo> fileInfoList = new ArrayList<>();
        RepoPath repoPath = new RepoPathImpl(repoKey, path);
        if (repoService.exists(repoPath)) {
            ItemInfo itemInfo = repoService.getItemInfo(repoPath);
            if (itemInfo.isFolder()) {
                List<ItemInfo> itemInfoList = repoService.getChildren(repoPath);
                for (ItemInfo info : itemInfoList) {
                    fileInfoList.add(createFileInfo(info.getRepoPath()));
                }
            } else {
                fileInfoList.add(createFileInfo(itemInfo.getRepoPath()));
            }

            return Response.ok()
                    .entity(fileInfoList)
                    .type(MediaType.APPLICATION_JSON_TYPE)
                    .build();
        } else {
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity("Repo path not exists")
                    .type(MediaType.APPLICATION_JSON_TYPE)
                    .build();
        }
    }

    @DELETE
    @Produces({MediaType.APPLICATION_JSON})
    @Path("remove/{repoKey: (?!_any)[^/]+}/{path: (?!_any).+}")
    public Response removeRepoByPath(@PathParam("repoKey") String repoKey, @PathParam("path") String path) {
        Response response = hasPermission();
        if (null != response) {
            return response;
        }

        WrongPath result = new WrongPath();
        Map<String, String[]> parameterMap = request.getParameterMap();
        if ((null != parameterMap) && (parameterMap.size() > 0)) {
            if (parameterMap.containsKey("versions")) {
                String[] versions = parameterMap.get("versions");
                for (String ver : versions) {
                    RepoPath repoPath;
                    if (path.endsWith("/")) {
                        repoPath = new RepoPathImpl(repoKey, path + ver);
                    } else {
                        repoPath = new RepoPathImpl(repoKey, path + "/" + ver);
                    }
                    if (repoService.exists(repoPath)) {
                        repoService.undeploy(repoPath);
                    } else {
                        result.add(repoPath.toString());
                    }
                }
            }
        } else {
            RepoPath repoPath = new RepoPathImpl(repoKey, path);
            if (repoService.exists(repoPath)) {
                repoService.undeploy(repoPath);
            } else {
                result.add(repoPath.toString());
            }
        }

        if (result.size() > 0) {
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity(result)
                    .type(MediaType.APPLICATION_JSON_TYPE)
                    .build();
        } else {
            return Response.ok("Success", MediaType.APPLICATION_JSON_TYPE).build();
        }
    }

    private Response hasPermission() {
        if (authorizationService.isAuthenticated() && !authorizationService.isAnonymous()) {
            return null;
        } else {
            if (centralConfig.getDescriptor().getSecurity().isHideUnauthorizedResources()) {
                return Response.status(Response.Status.NOT_FOUND).build();
            }
            return Response.status(Response.Status.UNAUTHORIZED).build();
        }
    }

    private FileInfo createFileInfo(RepoPath repoPath) {

        FileInfo fileInfo = new FileInfo();

        fileInfo.setFolder(repoPath.isFolder());
        fileInfo.setRepoKey(repoPath.getRepoKey());
        fileInfo.setPath(repoPath.getPath());

        ItemInfo itemInfo = null;
        if (repoPath.isFile()) {
            org.artifactory.fs.FileInfo file;
            file = repoService.getFileInfo(repoPath);

            fileInfo.setSize(file.getSize());
            fileInfo.setMd5(file.getMd5());
            fileInfo.setSha1(file.getSha1());

            ModuleInfo moduleInfo = repoService.getItemModuleInfo(repoPath);
            fileInfo.setGroupId(moduleInfo.getOrganization());
            fileInfo.setArtifactId(moduleInfo.getModule());
            fileInfo.setVersion(moduleInfo.getBaseRevision());
            fileInfo.setExt(moduleInfo.getExt());
            fileInfo.setClassifier(moduleInfo.getClassifier());

            itemInfo = file;
        }
        if (repoPath.isFolder()) {
            org.artifactory.fs.FolderInfo file;
            file = repoService.getFolderInfo(repoPath);
            itemInfo = file;
        }

        if (null != itemInfo) {
            fileInfo.setCreatedBy(itemInfo.getCreatedBy());
            fileInfo.setModifiedBy(itemInfo.getModifiedBy());
            fileInfo.setCreated(itemInfo.getCreated());
            fileInfo.setLastModified(itemInfo.getLastModified());
        }

        StatsInfo statsInfo = repoService.getStatsInfo(repoPath);
        if (null != statsInfo) {
            fileInfo.setDownloadCount(statsInfo.getDownloadCount());
            fileInfo.setLastDownloaded(statsInfo.getLastDownloaded());
        }

        if (repoService.hasProperties(repoPath)) {
            Properties properties = repoService.getProperties(repoPath);
            if (null != properties) {
                for (String key : properties.keySet()) {
                    Set<String> value = properties.get(key);
                    if (null != value) {
                        fileInfo.getProperties().put(key, value.toString());
                    }
                }
            }
        }

        return fileInfo;
    }

    public class FileInfo {

        private boolean isFolder;

        private String repoKey;
        private String path;
        private String createdBy;
        private long created;
        private String modifiedBy;
        private long lastModified;

        private long size;
        private String md5;
        private String sha1;

        private long downloadCount;
        private long lastDownloaded;

        private String groupId;
        private String artifactId;
        private String version;
        private String ext;
        private String classifier;

        private Map<String, String> properties = new HashMap<>();

        public long getLastModified() {
            return lastModified;
        }

        public void setLastModified(long lastModified) {
            this.lastModified = lastModified;
        }

        public long getCreated() {
            return created;
        }

        public void setCreated(long created) {
            this.created = created;
        }

        public String getModifiedBy() {
            return modifiedBy;
        }

        public void setModifiedBy(String modifiedBy) {
            this.modifiedBy = modifiedBy;
        }

        public String getCreatedBy() {
            return createdBy;
        }

        public void setCreatedBy(String createdBy) {
            this.createdBy = createdBy;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public String getRepoKey() {
            return repoKey;
        }

        public void setRepoKey(String repoKey) {
            this.repoKey = repoKey;
        }

        public Map<String, String> getProperties() {
            return properties;
        }

        public void setProperties(Map<String, String> properties) {
            this.properties = properties;
        }

        public boolean isFolder() {
            return isFolder;
        }

        public void setFolder(boolean folder) {
            isFolder = folder;
        }

        public long getDownloadCount() {
            return downloadCount;
        }

        public void setDownloadCount(long downloadCount) {
            this.downloadCount = downloadCount;
        }

        public long getLastDownloaded() {
            return lastDownloaded;
        }

        public void setLastDownloaded(long lastDownloaded) {
            this.lastDownloaded = lastDownloaded;
        }

        public long getSize() {
            return size;
        }

        public void setSize(long size) {
            this.size = size;
        }

        public String getMd5() {
            return md5;
        }

        public void setMd5(String md5) {
            this.md5 = md5;
        }

        public String getSha1() {
            return sha1;
        }

        public void setSha1(String sha1) {
            this.sha1 = sha1;
        }

        public String getGroupId() {
            return groupId;
        }

        public void setGroupId(String groupId) {
            this.groupId = groupId;
        }

        public String getArtifactId() {
            return artifactId;
        }

        public void setArtifactId(String artifactId) {
            this.artifactId = artifactId;
        }

        public String getVersion() {
            return version;
        }

        public void setVersion(String version) {
            this.version = version;
        }

        public String getExt() {
            return ext;
        }

        public void setExt(String ext) {
            this.ext = ext;
        }

        public String getClassifier() {
            return classifier;
        }

        public void setClassifier(String classifier) {
            this.classifier = classifier;
        }
    }

    public class WrongPath {

        private String message = "Repo path not exists";
        private List<String> paths = new ArrayList<>();

        public List<String> getPaths() {
            return paths;
        }

        public void setPaths(List<String> paths) {
            this.paths = paths;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public void add(String path) {
            paths.add(path);
        }

        public int size() {
            return paths.size();
        }
    }
}

 

 好了,你可以做自己想做的了眨眼

分享到:
评论

相关推荐

    搭建maven私服的软件

    在IT行业中,Maven是一个广泛使用的项目管理和综合工具,它帮助开发者管理依赖、构建项目以及...本文介绍了如何安装Nexus,创建Maven私服仓库,以及配置Maven使用这个私服。这将对团队协作和项目管理带来显著的提升。

    linux下搭建内网maven私服

    在 Maven 项目中,可以使用 distributionManagement 节点来部署 Jar 或 War 包到 Maven 私服仓库。例如,在 Pom.xml 文件中添加以下配置: &lt;id&gt;my-repo &lt;url&gt;...

    使用Nexus搭建Maven私服

    使用Nexus搭建Maven私服 标题:使用Nexus搭建Maven私服 描述:关于使用Nexus搭建Maven私服的开发文档。 标签:Nexus 搭建Maven 在实际的企业开发中经常会遇到的问题:在进行Maven项目开发时,所需要的构件都是...

    配置Maven私服配置文件setting.xml

    配置Maven私服时,需要修改Maven的默认配置文件,此文件已经配置好Maven私服模板,只需要修改IP和对应的端口号即可

    maven私服教程.zip

    Maven是一个在Java开发中广泛使用的...通过学习本教程,你将能够熟练地搭建和管理自己的Maven私服,为团队开发提供高效且安全的依赖管理环境。请务必仔细阅读每一步,理解其背后的原理,以便更好地应用到实际工作中。

    maven私服demo

    下面将详细讲解Maven私服的概念、作用以及如何设置和使用。 **Maven私服概念** Maven私服本质上是一个本地的Maven仓库,它可以是Nexus、Artifactory等专门的仓库管理软件,也可以是简单的HTTP服务器上的一系列目录...

    Windows 下Nexus搭建Maven私服

    ### Windows 下 Nexus 搭建 Maven 私服详解 #### 一、为什么使用 Nexus 在软件开发过程中,尤其是采用 Maven 构建管理的项目中,依赖管理是非常关键的一环。通常,开发人员需要从 Maven 中央仓库下载各种依赖库到...

    Nexus搭建Maven私服 +maven安装步骤

    这个时候,我们不得不为自己的团队搭建属于自己的 Maven 私服,这样既节省了网络带宽也会加速项目搭建的进程。 Nexus 的下载和安装 Nexus 的下载地址是 http://www.sonatype.org/nexus/go。下载完成后,解压缩到...

    阿里云maven私服jar包上传和拉取教程

    - 运行`mvn dependency:resolve`或`mvn install`命令,Maven会自动从私服仓库中拉取所需的jar包,并将其安装到本地仓库中供项目使用。 综上所述,通过上述步骤,你可以轻松地在阿里云环境中搭建一个maven私服,并...

    Maven Nexus 私服搭建

    Maven Nexus 私服搭建 从零开始,资源下载、安装指导、开发配置说明

    maven私服搭建文档

    maven私服搭建文档里简单介绍了maven私服的搭建及配置

    maven私服搭建.pdf

    附件是自己使用docker在内网和外网搭建Maven私服Nexus3所总结的文档,通过该文档可以在内网搭建属于自己的nexus私服,提高自己的生产力。

    maven私服nexus最新版本2.14

    配置 Maven 以使用 Nexus 私服通常涉及修改 `settings.xml` 文件,配置本地仓库地址、远程仓库地址以及代理仓库地址。同时,还需要在 Nexus 管理界面创建仓库并分配相应的权限。 ** 安装与运行 Nexus ** 安装 ...

    Maven私服.zip

    该压缩文件包含nexus-3.37.3-02-win64.zip、如何修改远程仓库地址.docx、如何在Windows下搭建Maven私服.docx和视频地址.txt等四个文件,主要介绍了如何搭建Maven私服,视频教程:...

    在CentOS7上用Nexus3搭建Maven私服.doc

    在本文中,我们将详细介绍如何在CentOS7操作系统上使用Nexus3搭建Maven私有仓库。首先,确保系统已经安装了Java Development Kit (JDK) 8和Maven3。安装这两个组件的具体步骤可以通过搜索引擎获取。 一、所需环境 ...

    linux-maven-maven私服nexus安装文档

    Linux Maven Maven私服Nexus安装文档主要涵盖了在CentOS操作系统上安装和配置Maven以及搭建Maven私有仓库Nexus的详细步骤。以下是整个过程的详细说明: 1. **安装Maven** - **确认安装源**:首先,你需要检查你的...

    maven 私服 nexus3 安装包

    总结,Nexus3 是一个强大且灵活的 Maven 私服解决方案,虽然在使用过程中可能会遇到各种问题,但通过了解其工作原理和配置细节,我们完全可以克服这些困难,充分利用它来优化软件开发流程。在日常开发中,不断学习和...

    搭建Maven私服,并配置Nexus环境

    然而,当项目涉及大量的私有库或者内部依赖时,直接使用公共Maven仓库可能存在效率和安全问题。这时,搭建一个Maven私服就显得尤为重要。Nexus是Sonatype公司推出的开源Maven私服,它不仅可以作为内部依赖仓库,还...

    建立Maven私服 – Nexus下载、安装、配置、使用

    Maven私服是一种私有的Maven仓库,用于存储和管理项目依赖...通过以上步骤,你就成功建立了自己的Maven私服,并学会了如何下载、安装、配置和使用Nexus。这将极大地优化你的开发流程,减少网络延迟,提升团队协作效率。

Global site tag (gtag.js) - Google Analytics