Automatically Refresh Eclipse Projects before Build

The Eclipse CDT build system automatically scans the files in my project folders and adds them to the list of files to be built. That works great if files are added through Eclipse and its plugins: That way Eclipse is notified and aware, and has the files added. But what if I have added files externally (outside of Eclipse)? how can I make Eclipse aware of it?

File added but not shown

File added but not shown in Eclipse Project View

Outline

A new file could have been added by an external script, e.g. to generate new source file with a pre-build step with a time stamp or any other build related information. If that file is always new and does have a different file name, Eclipse is not aware of it. So I need to make sure the Eclipse project ‘knows’ about it before the build. So I need to ‘refresh’ the project or workspace before the build.

In this post I’m using Eclipse Neon 4.6 with the example of MCUXpresso IDE, but any other Eclipse distribution or version can be used.

Refresh Menu

Obviously, there is the ‘Refresh’ menu to manually update the list of files:

Refresh Context menu

Refresh Context menu

And then the new file will show up:

New File After Refresh

New File After Refresh

But this is a manual process. What I would like to do this automatically before every build.

Refresh Policy?

There is the Eclipse ‘Refresh Policy’ feature. Unfortunately, that refresh is only *after* the build:

Eclipse Refresh Policy for Project

Eclipse Refresh Policy for Project

So this does not help.

Workspace Refresh?

There is are two settings in the workspace which usually are not turned on:

Workspace refresh

Workspace refresh

This will do extra polling in the background, or when I access the files/folders in the project view.

It works that way, however because of the polling this adds more load to my host system, especially with many open projects in the workspace. Plus it is for *all* projects in the workspace. What I need is something just for a single project.

Custom Builder for Refresh

The solution I’m using is to add a custom action to the project ‘Builders’ setting:

Force Refresh Action in Builder

Force Refresh Action in Builder

Use the ‘New…’ button to add a new builder action, and use afterwards the ‘Up’ button to have it first in the list so it gets executed before the ‘CDT Builder’:

New Builder Action Program

New Builder Action Program

As program to execute specify something, for a refresh it really does not matter. So it could be any ‘dummy’ program. What I’m using on my side is the ‘echo’ program from the GNU ARM Eclipse build tools suite:

Program

Program

What it does it prints the argument string to the console like this:

Console Output

Console Output

So the program does not matter. What matters are the refresh settings for that action:

Refresh Selected Resource

Refresh Selected Resource

With this, the action (either a dummy action or the action to create a new file with a timestamp) will cause a refresh of the selected (project) folder and all folders inside (recursively). Done :-).

Summary

I love Eclipse because it can do pretty much everything. With the custom builder settings I can do custom actions before (or after) the build without the need to manually write make or build files (but make files are still very, very useful).

Happy Refreshing 🙂

Advertisements

7 thoughts on “Automatically Refresh Eclipse Projects before Build

  1. “I love Eclipse because it can do pretty much everything” – interesting you say this; I found the “walled garden” file system of Eclipse to be about it’s worst feature. In CodeWarrior if I wanted to create a new variant of a project, I just copy the directory; if I want to change files, I just change them. In Eclipse, all these simple file things become a chore, for reasons that I don’t know or see the benefit of.
    Admitted, I did find ways to get around all the barriers, so I suppose it *can* do everything; just maybe not as easily as it should be.

    Like

    • Hmm, I don’t see any ‘walled garden’ file system in Eclipse?
      If I want a new project, then I can simply copy it too (Ctrl-C, Ctrl-V) in Eclipse (or outside Eclipse). It is true that Eclipse uses (as other IDEs) information in .project and .cproject files, but as they are text files, they can be easily managed too. However, what I agree is a constant pain is the separation between ‘Eclipse Framework’ and ‘CDT’ (for C/C++ development): the framework does not know that CDT does and vice-versa. This for example is the source of pain for the debug configurations, where the connection between the editor/framework and the debugger is very thin. Same between build tools and editor. Maybe this is what you observe?

      Like

      • In that case, I recommend that you enable the refresh in the workspace: that way it poll the file system for any changes. That’s the same thing CodeWarrior (classic and Eclipse based) can do too.

        Like

  2. Thank you. It helped me a a lot. There is only one issue. echo.exe cleans console and prints “refreshing project”. Is there any way of appending text on console because I also want to see file sizes?

    Like

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s