使用插件管理Maven配置文件今天开发一个插件来管理Maven的配置setting.xml文件。 开始 先创建Actio
今天开发一个插件来管理Maven
的配置setting.xml
文件。
开始
先创建Action
Action
的Id
,名字要唯一,用于在菜单组中绑定Action
类的名字- 插件展示的名字
- 描述
- 展示在哪个菜单组中
- 快捷键
- 在菜单组中的位置
这里创建完成后,会在 META-INF/plugin.xml
文件中自动生成对应的配置,icon
是后面加的,自动生成的没有。
界面
在idea
中画界面很简单
右键选择Preview
,还可以预览。
预览只能看看UI
界面,按钮点击是没有反应的
编写代码
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
的方式运行。
这个命令是生成插件。
插件
好消息是这个插件已经发布到插件市场了,直接搜索安装就可以啦。
看看效果:
这两个按钮都可以启动插件
选择第一个目录
如果有很多,支持滚动
选择第二个目录
点击确认切换
Maven
中的配置文件已经被替换了。
源代码在插件页面有 plugins.jetbrains.com/plugin/2528…
最后
希望大家多多支持。欢迎大家评分和评论,如果有好的建议可以评论。
转载自:https://juejin.cn/post/7426693885332881443