Grunt plugin for updating the eclipse java project

Background:

I have a java project that uses lesscss. I use gruntwith grunt-contrib-watchand grunt-contrib-less to compile my.less files to.css`.

Everything works well.

The problem is that in order for the eclipse tomcat server to start servicing updated files .css, I need to update the project in eclipse.

I wandered if there is a way to make the eclipse refresh as part of the loop watchin grunt? or in fact, there is a way to invoke an open eclipse project (given that I know its path if it helps) update with grunt.

Connecting to an hourly cycle is not difficult, and you can probably do it by changing mine Gruntfile.jsto:

    watch: {
        styles: {
            // Which files to watch (all .less files recursively in the less directory)
            files: ['../WebContent/less/**/*.less'],
            tasks: ['less'],
            options: {
              nospawn: true
            }
        },

at

    watch: {
        styles: {
            // Which files to watch (all .less files recursively in the less directory)
            files: ['../WebContent/less/**/*.less'],
            tasks: ['less','updateEclipseTask'],
            options: {
              nospawn: true
            }
        },
+4
2

Preferences > General > Workspace > Refresh using native hooks or polling. , , . , , .

, , , , eclipse-remote-control, , Eclipse (Run > External Tools > External Tools Configurations...), "", "" Eclipse .

eclipse-remote-control grunt, grunt-shell.

+4

, Eclipse , - Grunt, Eclipse.

Eclipse, , , Tomcat, Eclipse, .

, "Refresh using native hooks and polling", , Eclipse, - .

, - .

  • Eclipse Eclipse. RapidFileRefresher.
  • .
  • ( PROJECT_NAMES RapidRefreshProvider) , , FOLDERS ProjectMonitorJob.
  • Manifest.MF, "" " ". " " . , .
  • "". Eclipse .
  • , " " "" > "" > " "

, , "" > " Eclipse", " ", " ", , .

, :

  • RefreshProvider (RapidRefreshProvider) .
  • RefreshMonitor
  • , 350 ( ), , , . .

, , , .

Update

Github: https://github.com/peterjkirby/RapidFileRefreshPlugin

( , ), Eclipse . Tomcat, Eclipse, , .

Manifest.mf

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: RapidFileRefresher
Bundle-SymbolicName: rfr.RapidFileRefresher;singleton:=true
Bundle-Version: 1.0.2
Bundle-Activator: rfr.core.Activator
Require-Bundle: org.eclipse.core.runtime
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.core.internal.refresh,
 org.eclipse.core.internal.resources.mapping,
 org.eclipse.core.resources,
 org.eclipse.core.resources.refresh
Bundle-Vendor: PeterKirby
Export-Package: rfr.core

plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         id="RapidFileRefresher"
         point="org.eclipse.core.resources.refreshProviders">
      <refreshProvider
            class="rfr.core.RapidFileRefresher"
            name="RapidFileRefresher">
      </refreshProvider>   </extension>

</plugin>

Activator.java

public class Activator extends Plugin {

    private static Activator INSTANCE;

    public static final String PLUGIN_ID = "RapidFileRefresher";

    public Activator() {
        super();
        INSTANCE = this;
    }

    public static Activator getInstance() {
        return INSTANCE;
    }

    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        super.stop(context);
    }

}

RapidRefreshProvider.java

public class RapidRefreshProvider extends RefreshProvider {

    private static final String[] PROJECTS_NAMES = {
        "YOUR_PROJECT_NAMES"
    };

    private Set<String> projects = new HashSet<>();

    public RapidRefreshProvider() {
        projects.addAll(Arrays.asList(PROJECTS_NAMES));
    }


    @Override
    public IRefreshMonitor installMonitor(IResource resource, IRefreshResult result) {

        // only monitor resources that are projects
        if (resource.getType() != IResource.PROJECT) return null;

        IProject project = (IProject) resource;

        // only monitor the projects in PROJECT_NAMES
        if (!projects.contains(project.getName())) return null; 

        RapidRefreshMonitor monitor = new RapidRefreshMonitor(resource, result);
        monitor.start();

        return monitor;
    }

}

RapidRefreshMonitor.java

public class RapidRefreshMonitor implements IRefreshMonitor {

    // wait INITIAL_DELAY seconds before starting to track changes 
    // in order to allow eclipse to startup faster.
    private static final int INITIAL_DELAY = 1000 * 20;
    private ProjectMonitorJob job;
    private IResource resource;
    private IRefreshResult result;

    public RapidRefreshMonitor(IResource resource, IRefreshResult result) {
        this.resource = resource;
        this.result = result;       
    }

    public void start() {
        job = new ProjectMonitorJob(resource, result);
        job.schedule(INITIAL_DELAY);
    }


    @Override
    public void unmonitor(IResource resource) {
        job.stop();     
    }

}

ProjectMonitorJob.java

public class ProjectMonitorJob extends Job {

    private static final String JOB_NAME = "ProjectMonitorJob";
    private static final String[] FOLDERS = {
            "src/main/webapp/resources"
    };
    // fires about 3 times per second.
    private static final int DELAY = 350;
    private boolean RUNNING = true;

    private IResource resource;
    private IRefreshResult result;

    public ProjectMonitorJob(IResource resource, IRefreshResult result) {
        super(Activator.PLUGIN_ID + " - " + JOB_NAME);
        this.resource = resource;
        this.result = result;

    }

    @Override
    protected IStatus run(IProgressMonitor monitor) {

        if (RUNNING) {

            IProject project = (IProject) resource;

            for (String folderPath : FOLDERS) {
                monitor(project, folderPath);
            }

            // schedule the next run
            schedule(DELAY);
        }

        return Status.OK_STATUS;
    }

    private void monitor(IProject project, String folderPath) {
        IFolder folder = project.getFolder(new Path(folderPath));

        if (folder.exists()) {
            ResourceTraverser traverser = new ResourceTraverser(new ResourceChangeEvaluator());

            try {
                traverser.traverse(folder, result);
            } catch (CoreException e) {}
        }
    }

    public void stop() {
        RUNNING = false;
    }

}

ResourceChangeEvaluator.java

public class ResourceChangeEvaluator {

    public ResourceChangeEvaluator() {}     

    public boolean changed(IFolder folder) {
        if (folder == null) return false;
        return !folder.isSynchronized(IResource.DEPTH_ONE);
    }

    public boolean changed(IFile file) throws CoreException {
        if (file == null) return false;
        if (!file.exists()) return false;
        if (file.isSynchronized(IResource.DEPTH_ZERO)) return false;

        return true;
    }

}

ResourceTraverser.java

public class ResourceTraverser {

    private ResourceChangeEvaluator resourceChangeEvaluator;

    public ResourceTraverser(ResourceChangeEvaluator resourceChangeEvaluator) {
        this.resourceChangeEvaluator = resourceChangeEvaluator;
    }

    public void traverse(IFolder folder, IRefreshResult refreshResult) throws CoreException {

        IResource[] contents = folder.members();
        Activator activator = Activator.getInstance();

        if (resourceChangeEvaluator.changed(folder)) {
            refreshResult.refresh(folder);
        }

        for (IResource resource : contents) {                                   
            if (resource.getType() == IResource.FILE) {
                IFile file = (IFile) resource;              

                    if (resourceChangeEvaluator.changed(file)) {
                        refreshResult.refresh(file);
                        activator.getLog().log(new Status(IStatus.INFO, Activator.PLUGIN_ID, "File change detected. Refreshing " + file.getName()));
                    }


            } else if (resource.getType() == IResource.FOLDER) {
                IFolder subfolder = (IFolder) resource;

                // only continue traversing if a folder has not changed. If a folder has changed
                // the refresh event at that folder should be enough to force synchronization 
                // Plus, other changes at a depth > 1 will be detected on the next pass.
                // This is more a performance optimization than anything else. 
                if (resourceChangeEvaluator.changed(subfolder)) {
                    refreshResult.refresh(subfolder);
                    activator.getLog().log(new Status(IStatus.INFO, Activator.PLUGIN_ID, "Folder change detected. Refreshing " + subfolder.getName()));
                } else {
                    traverse((IFolder) resource, refreshResult);
                }
            }
        }
    }

}
+1

Source: https://habr.com/ru/post/1529665/


All Articles