使用插件管理Maven配置文件

180 阅读2分钟

今天开发一个插件来管理Maven的配置setting.xml文件。

开始

先创建Action

image-20240911002009318.png

image-20240911002952307.png

  1. ActionId,名字要唯一,用于在菜单组中绑定
  2. Action类的名字
  3. 插件展示的名字
  4. 描述
  5. 展示在哪个菜单组中
  6. 快捷键
  7. 在菜单组中的位置

这里创建完成后,会在 META-INF/plugin.xml 文件中自动生成对应的配置,icon是后面加的,自动生成的没有。

image-20240911004536820.png

界面

idea中画界面很简单

image-20240911001824567.png

右键选择Preview,还可以预览。

image-20240911003305611.png

预览只能看看UI界面,按钮点击是没有反应的

image-20240911003411247.png

编写代码

MavenSettingsHandlerAction

public class MavenSettingsHandlerAction extends AnAction {
​
  @Override
  public void actionPerformed(AnActionEvent e) {
    CommonUtil.init(e);
  }
}

CommonUtil中的init方法展示界面

CommonUtil

public class CommonUtil {
​
  /**
   * 初始化界面
   */
  public static void init(AnActionEvent e) {
    MavenSettingForm dialog = new MavenSettingForm(e.getProject());
    dialog.pack();
    //设置窗口居中
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);
  }
​
  public static void initConfig(JTextField setPath, JTextField confPath) {
    PropertiesComponent.getInstance().setValue("setPath", setPath.getText());
    PropertiesComponent.getInstance().setValue("confPath", confPath.getText());
  }
​
  public static String getSetPath() {
    return PropertiesComponent.getInstance().getValue("setPath");
  }
​
  public static String getConfPath() {
    return PropertiesComponent.getInstance().getValue("confPath");
  }
​
}

这里因为配置的东西不多,就偷了个懒,使用PropertiesComponent来存储了,这个保存在idea 的配置文件中。配了一次,所有的项目中都生效。

FileChooseUtil

public class FileChooseUtil {
​
    private final Project project;
    private final FileEditorManager fileEditorManager;
​
    private FileChooseUtil(final Project project) {
        this.project = project;
        this.fileEditorManager = FileEditorManager.getInstance(project);
    }
​
    public static FileChooseUtil getInstance(Project project) {
        if (project == null) {
            return null;
        }
        return new FileChooseUtil(project);
    }
​
    public VirtualFile showSingleFolderSelectionDialog(String title, VirtualFile toSelect, VirtualFile... roots) {
        if (title == null) {
            return null;
        }
        FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
        descriptor.setTitle(title);
        if (roots != null) {
            descriptor.setRoots(roots);
        }
        return FileChooser.chooseFile(descriptor, this.project, toSelect);
    }
​
}

这个是文件选择器,是idea提供的,样式比Java原生的好看

MavenSettingForm

这个类对应前面的界面

核心方法:

这个方法是选择文件夹。

private void onSelectConf() {
  FileChooseUtil uiComponentFacade = FileChooseUtil.getInstance(project);
  VirtualFile baseDir = null;
  if (project != null) {
    baseDir = project.getBaseDir();
  }
  String existPath = this.confPath.getText();
  if (StringUtils.isNotBlank(existPath)) {
    VirtualFileManager instance = VirtualFileManager.getInstance();
    baseDir = instance.findFileByUrl("file://" + existPath);
  }
  final VirtualFile vf = uiComponentFacade.showSingleFolderSelectionDialog("选择Maven conf文件夹路径", baseDir, baseDir);
  if (vf == null) {
    return;
  }
  if (!vf.isDirectory()) {
    JOptionPane.showMessageDialog(this.contentPane, "请选择文件夹!", "错误", JOptionPane.ERROR_MESSAGE);
    return;
  }
  this.confPath.setText(vf.getPath());
}

这个方法是点击确定按钮的时候,先把原来的删掉,然后把选择的复制过去,并且名字是settings.xml

private void onOK() {
  CommonUtil.initConfig(setPath, confPath);
  File selectedValue = this.fileList.getSelectedValue();
  String name = selectedValue.getName();
  if (!name.endsWith(".xml")) {
    JOptionPane.showMessageDialog(this.contentPane, "请选择xml类型文件!", "错误", JOptionPane.ERROR_MESSAGE);
    return;
  }
  String preConf = this.confPath.getText() + File.separator + "settings.xml";
  File prfConfFile = new File(preConf);
  if (prfConfFile.exists()) {
    prfConfFile.delete();
  }
  File nowConfFile = new File(preConf);
  try {
    FileUtils.copyFile(selectedValue, nowConfFile);
  } catch (IOException e) {
    e.printStackTrace();
  }
  JOptionPane.showMessageDialog(this.contentPane, "修改成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
  dispose();
}

接下来可以运行看看效果,这个是运行的命令,右键可以选择使用Debug的方式运行。

image-20240911004708900.png

这个命令是生成插件。

image-20240911004753168.png

插件

好消息是这个插件已经发布到插件市场了,直接搜索安装就可以啦。

image-20240911005046819.png

image-20240911005102465.png 看看效果:

image-20241018161040731.png

image-20241018161105158.png

这两个按钮都可以启动插件

选择第一个目录

image-20240911005352937.png

image-20240911005242723.png

如果有很多,支持滚动

image-20240911010242845.png

选择第二个目录

image-20240911005441204.png

点击确认切换

image-20240911005512690.png

Maven中的配置文件已经被替换了。

源代码在插件页面有 plugins.jetbrains.com/plugin/2528…

image-20240911010012022.png

最后

希望大家多多支持。欢迎大家评分和评论,如果有好的建议可以评论。