maven-plugin(idea)-2

package jacco;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class GenerateCoverageReportAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent event) {
        Project project = event.getProject();
        if (project == null) {
            return;
        }

        VirtualFile projectFile = project.getProjectFile();
        if (projectFile == null) {
            return;
        }

        VirtualFile baseDir = project.getBaseDir();
        if (baseDir == null) {
            return;
        }


        MyToolWindowPanel1 panel = getToolWindowPanel(project);

        String mavenExecutablePath = getMavenExecutablePath();
        if (StringUtil.isEmptyOrSpaces(mavenExecutablePath)) {
            Messages.showErrorDialog("Cannot find Maven executable", "Error");
            return;
        }

        List<String> command = new ArrayList<>();
        command.add(mavenExecutablePath);
        command.add("clean");

        try {
            ProcessBuilder builder = new ProcessBuilder(command);
            builder.directory(new File(baseDir.getPath()));
            builder.redirectErrorStream(true);

            Process process = builder.start();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                panel.appendText(line + "\n");
            }

            process.waitFor();
        } catch (IOException | InterruptedException e) {
            Messages.showErrorDialog("Failed to execute Maven command: " + e.getMessage(), "Error");
        }
    }

    private String getMavenExecutablePath() {
        String mavenExecutable = SystemInfo.isWindows ? "mvn.cmd" : "mvn";
        String[] pathVariables = System.getenv("PATH").split(System.getProperty("path.separator"));

        for (String pathVariable : pathVariables) {
            String executablePath = pathVariable + "/" + mavenExecutable;
            if (isExecutable(executablePath)) {
                return executablePath;
            }
        }

        return "";
    }

    private boolean isExecutable(String path) {
        ProcessBuilder builder = new ProcessBuilder("sh", "-c", "test -x " + path);
        try {
            Process process = builder.start();
            process.waitFor();
            return process.exitValue() == 0;
        } catch (IOException | InterruptedException e) {
            return false;
        }
    }

    private MyToolWindowPanel1 getToolWindowPanel(Project project) {
        MyToolWindowPanel1 panel = null;
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        ToolWindow toolWindow = toolWindowManager.getToolWindow("My Plugin Tool Window");
        if (toolWindow != null) {
            Content content = toolWindow.getContentManager().getContent(0);
            if (content != null) {
                panel = (MyToolWindowPanel1) content.getComponent();
            }
        }
        return panel;
    }
}





在这里插入代码片
package jacco;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.content.ContentManager;
import com.intellij.util.ui.update.Activatable;
import com.intellij.util.ui.update.UiNotifyConnector;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

public class MyPluginToolWindow implements ProjectComponent, ToolWindowFactory {
    private Project project;
    private ToolWindow toolWindow;
    private MyToolWindowPanel1 myToolWindowPanel;

    private UiNotifyConnector uiNotifyConnector;

    public MyPluginToolWindow(Project project) {
        this.project = project;
    }

    @Override
    public void initComponent() {

//        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
//        ContentFactory contentFactory = ContentFactory.getInstance();
//
//        myToolWindowPanel = new MyToolWindowPanel1();
//        Content content = contentFactory.createContent(myToolWindowPanel, "My Tool Window", false);
//        toolWindow = toolWindowManager.registerToolWindow("My Plugin Tool Window", false, ToolWindowAnchor.RIGHT);
//        ContentManager contentManager = toolWindow.getContentManager();
//        contentManager.addContent(content);
    }



    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
//        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
//        ContentFactory contentFactory = ContentFactory.getInstance();
//
//        myToolWindowPanel = new MyToolWindowPanel1();
//        Content content = contentFactory.createContent(myToolWindowPanel, "My Tool Window", false);
//        toolWindow = toolWindowManager.registerToolWindow("My Plugin Tool Window", false, ToolWindowAnchor.RIGHT);
//        ContentManager contentManager = toolWindow.getContentManager();
//        contentManager.addContent(content);
    }

    @Override
    public void disposeComponent() {

        // 释放组件时执行的操作,例如注销工具窗口

    }

    @Override
    public void projectOpened() {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        ContentFactory contentFactory = ContentFactory.getInstance();

        myToolWindowPanel = new MyToolWindowPanel1();
        Content content = contentFactory.createContent(myToolWindowPanel, "My Tool Window", false);
        toolWindow = toolWindowManager.registerToolWindow("My Plugin Tool Window", false, ToolWindowAnchor.RIGHT);
        ContentManager contentManager = toolWindow.getContentManager();
        contentManager.addContent(content);
        toolWindow.show();
    }

    @Override
    public void projectClosed() {

        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        toolWindowManager.unregisterToolWindow("My Tool Window");

    }


}




 class MyToolWindowPanel1 extends JPanel {

     private final JTextArea textArea;
    public MyToolWindowPanel1() {
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(500, 300));

        textArea = new JTextArea();
        textArea.setEditable(false);

        JScrollPane scrollPane = new JBScrollPane(textArea);
        add(scrollPane, BorderLayout.CENTER);
    }


     public void appendText(String text) {
         SwingUtilities.invokeLater(() -> textArea.append(text));
     }
}