Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Introducing builder for existing projects
Introducing builder for existing projects [message #723089] Wed, 07 September 2011 15:31 Go to next message
Alex Kravets is currently offline Alex KravetsFriend
Messages: 546
Registered: November 2009
Senior Member
Hello,

In our application there are projects that are created by users, these projects do not have builders associated with them. In next release we added builders. This change will be okay for newly created projects, but existing projects don't have builders defined in .project file, thus builders do not work for these projects.

One way of solving this is to examine each project's .project file and see if builder entry exists, if not - add it. Is there a better way to do this, as it sounds like a pretty common issue that might arise in Eclipse application.

Thanks,
Alex
Re: Introducing builder for existing projects [message #723103 is a reply to message #723089] Wed, 07 September 2011 15:59 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31467
Registered: July 2009
Senior Member
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Alex,<br>
<br>
Well, you wouldn't look at the .project file directly, you'd use
APIs to to check whether the nature you expect to be there is
present and whether the builders are there.  Kind of like we do when
we generate a project and want all the GWT builders to be there...<br>
<br>
<small>  private static class EclipseHelper<br>
  {<br>
    public static IProject createEMFProject<br>
      (IPath javaSource,<br>
       IPath projectLocationPath,<br>
       List&lt;IProject&gt; referencedProjects,<br>
       Monitor monitor,<br>
       int style,<br>
       List&lt;?&gt; pluginVariables)<br>
    {<br>
      return createEMFProject(javaSource, projectLocationPath ==
null ? null : URI.createFileURI(projectLocationPath.toOSString()),
referencedProjects, monitor, style, pluginVariables);<br>
    }<br>
<br>
    public static IProject createEMFProject<br>
      (IPath javaSource,<br>
       URI projectLocationURI,<br>
       List&lt;IProject&gt; referencedProjects,<br>
       Monitor monitor,<br>
       int style,<br>
       List&lt;?&gt; pluginVariables)<br>
    {<br>
      IProgressMonitor progressMonitor =
BasicMonitor.toIProgressMonitor(monitor);<br>
      String projectName = javaSource.segment(0);<br>
      IProject project = null;<br>
      try<br>
      {<br>
        List&lt;IClasspathEntry&gt; classpathEntries = new
UniqueEList&lt;IClasspathEntry&gt;();<br>
  <br>
        progressMonitor.beginTask("", 10);<br>
        progressMonitor.subTask<br>
          (CodeGenEcorePlugin.INSTANCE.getString<br>
             ("_UI_CreatingEMFProject_message", <br>
              new Object [] { projectName, projectLocationURI !=
null ? projectLocationURI.toString() : projectName }));<br>
        IWorkspace workspace = ResourcesPlugin.getWorkspace();<br>
        project = workspace.getRoot().getProject(projectName);<br>
  <br>
        // Clean up any old project information.<br>
        //<br>
        if (!project.exists())<br>
        {<br>
          URI location = projectLocationURI;<br>
          if (location == null)<br>
          {<br>
            location =
URI.createFileURI(workspace.getRoot().getLocation().append(projectName).toOSString());<br>
          }<br>
          location = location.appendSegment(".project");<br>
          File projectFile = new File(location.toString());<br>
          if (projectFile.exists())<br>
          {<br>
            projectFile.renameTo(new File(location.toString() +
".old"));<br>
          }<br>
        }<br>
  <br>
        IJavaProject javaProject = JavaCore.create(project);<br>
        IProjectDescription projectDescription = null;<br>
        if (!project.exists())<br>
        {<br>
          projectDescription =
ResourcesPlugin.getWorkspace().newProjectDescription(projectName);<br>
          if (projectLocationURI != null)<br>
          {<br>
            projectDescription.setLocationURI(new
java.net.URI(projectLocationURI.toString()));<br>
          }<br>
          project.create(projectDescription, new
SubProgressMonitor(progressMonitor, 1));<br>
          project.open(new SubProgressMonitor(progressMonitor,
1));<br>
        }<br>
        else <br>
        {<br>
          projectDescription = project.getDescription();<br>
          project.open(new SubProgressMonitor(progressMonitor,
1));<br>
          if (project.hasNature(JavaCore.NATURE_ID))<br>
          {<br>
           
classpathEntries.addAll(Arrays.asList(javaProject.getRawClasspath()));<br>
          }<br>
        }<br>
<br>
        boolean isInitiallyEmpty = classpathEntries.isEmpty();<br>
  <br>
        {<br>
          if (referencedProjects.size() != 0 &amp;&amp; (style
&amp; (EMF_PLUGIN_PROJECT_STYLE | EMF_EMPTY_PROJECT_STYLE)) == 0)<br>
          {<br>
            projectDescription.setReferencedProjects<br>
              (referencedProjects.toArray(new
IProject[referencedProjects.size()]));<br>
            for (IProject referencedProject : referencedProjects)<br>
            {<br>
              IClasspathEntry referencedProjectClasspathEntry =
JavaCore.newProjectEntry(referencedProject.getFullPath());<br>
             
classpathEntries.add(referencedProjectClasspathEntry);<br>
            }<br>
          }<br>
  <br>
          String [] natureIds = projectDescription.getNatureIds();<br>
          if (natureIds == null)<br>
          {<br>
            natureIds = <br>
              ((style &amp; EMF_GWT_PROJECT_STYLE) == 0 ? <br>
                  new String [] { JavaCore.NATURE_ID,
"org.eclipse.pde.PluginNature" } :<br>
                  (style &amp; EMF_EDITOR_PROJECT_STYLE) == 0 ? <br>
                    new String [] { JavaCore.NATURE_ID,
"org.eclipse.pde.PluginNature",
"com.google.gwt.eclipse.core.gwtNature" } :<br>
                    new String [] <br>
                      { JavaCore.NATURE_ID, <br>
                        "org.eclipse.pde.PluginNature", <br>
                        "com.google.gwt.eclipse.core.gwtNature", <br>
                       
"com.google.appengine.eclipse.core.gaeNature" });<br>
          }<br>
          else<br>
          {<br>
            if (!project.hasNature(JavaCore.NATURE_ID))<br>
            {<br>
              String [] oldNatureIds = natureIds;<br>
              natureIds = new String [oldNatureIds.length + 1];<br>
              System.arraycopy(oldNatureIds, 0, natureIds, 0,
oldNatureIds.length);<br>
              natureIds[oldNatureIds.length] = JavaCore.NATURE_ID;<br>
            }<br>
            if
(!project.hasNature("org.eclipse.pde.PluginNature"))<br>
            {<br>
              String [] oldNatureIds = natureIds;<br>
              natureIds = new String [oldNatureIds.length + 1];<br>
              System.arraycopy(oldNatureIds, 0, natureIds, 0,
oldNatureIds.length);<br>
              natureIds[oldNatureIds.length] =
"org.eclipse.pde.PluginNature";<br>
            }<br>
            if ((style &amp; EMF_GWT_PROJECT_STYLE) != 0
&amp;&amp;
!project.hasNature("com.google.gwt.eclipse.core.gwtNature"))<br>
            {<br>
              String [] oldNatureIds = natureIds;<br>
              natureIds = new String [oldNatureIds.length + 1];<br>
              System.arraycopy(oldNatureIds, 0, natureIds, 0,
oldNatureIds.length);<br>
              natureIds[oldNatureIds.length] =
"com.google.gwt.eclipse.core.gwtNature";<br>
            }<br>
            if ((style &amp; EMF_GWT_PROJECT_STYLE) != 0
&amp;&amp; (style &amp; EMF_EDITOR_PROJECT_STYLE) != 0 &amp;&amp;
!project.hasNature("com.google.appengine.eclipse.core.gaeNature"))<br>
            {<br>
              String [] oldNatureIds = natureIds;<br>
              natureIds = new String [oldNatureIds.length + 1];<br>
              System.arraycopy(oldNatureIds, 0, natureIds, 0,
oldNatureIds.length);<br>
              natureIds[oldNatureIds.length] =
"com.google.appengine.eclipse.core.gaeNature";<br>
            }<br>
          }<br>
          projectDescription.setNatureIds(natureIds);<br>
  <br>
          ICommand [] builders =
projectDescription.getBuildSpec();<br>
          if (builders == null)<br>
          {<br>
            builders = new ICommand [0];<br>
          }<br>
          boolean hasGWTBuilder = false;<br>
          int enhancerBuilderIndex = -1;<br>
          for (int i = 0; i &lt; builders.length; ++i)<br>
          {<br>
            if
("org.eclipse.emf.codegen.ecore.GWTBuilder".equals(builders[i].getBuilderName()))<br>
            {<br>
              hasGWTBuilder = true;<br>
            }<br>
            if
("com.google.appengine.eclipse.core.enhancerbuilder".equals(builders[i].getBuilderName()))<br>
            {<br>
              enhancerBuilderIndex = i;<br>
            }<br>
          }<br>
          if ((style &amp; EMF_GWT_PROJECT_STYLE) != 0 &amp;&amp;
(style &amp; EMF_EDITOR_PROJECT_STYLE) != 0 &amp;&amp;
!hasGWTBuilder)<br>
          {<br>
            ICommand [] oldBuilders = builders;<br>
            builders = new ICommand [oldBuilders.length + 1];<br>
            System.arraycopy(oldBuilders, 0, builders, 0,
oldBuilders.length);<br>
            builders[oldBuilders.length] =
projectDescription.newCommand();<br>
           
builders[oldBuilders.length].setBuilderName("org.eclipse.emf.codegen.ecore.GWTBuilder");<br>
          }<br>
          if (enhancerBuilderIndex != -1)<br>
          {<br>
            ICommand [] oldBuilders = builders;<br>
            builders = new ICommand [oldBuilders.length - 1];<br>
            System.arraycopy(oldBuilders, 0, builders, 0,
enhancerBuilderIndex);<br>
            System.arraycopy(oldBuilders, enhancerBuilderIndex +
1, builders, enhancerBuilderIndex, oldBuilders.length -
enhancerBuilderIndex - 1);<br>
          }<br>
          projectDescription.setBuildSpec(builders);<br>
          project.setDescription(projectDescription, new
SubProgressMonitor(progressMonitor, 1));<br>
  <br>
          IContainer sourceContainer = project;<br>
          if (javaSource.segmentCount() &gt; 1)<br>
          {<br>
            IPath sourceContainerPath =
javaSource.removeFirstSegments(1).makeAbsolute();<br>
            sourceContainer =
project.getFolder(sourceContainerPath);<br>
            if (!sourceContainer.exists())<br>
            {<br>
              for (int i = sourceContainerPath.segmentCount() - 1;
i &gt;= 0; i--)<br>
              {<br>
                sourceContainer =
project.getFolder(sourceContainerPath.removeLastSegments(i));<br>
                if (!sourceContainer.exists())<br>
                {<br>
                  ((IFolder)sourceContainer).create(false, true,
new SubProgressMonitor(progressMonitor, 1));  <br>
                }<br>
              }<br>
            }<br>
<br>
            IClasspathEntry sourceClasspathEntry =
JavaCore.newSourceEntry(javaSource);<br>
            for (Iterator&lt;IClasspathEntry&gt; i =
classpathEntries.iterator(); i.hasNext(); )<br>
            {<br>
              IClasspathEntry classpathEntry = i.next();<br>
              if (classpathEntry.getPath().isPrefixOf(javaSource))<br>
              {<br>
                i.remove();<br>
              }<br>
            }<br>
            classpathEntries.add(0, sourceClasspathEntry);<br>
          }<br>
  <br>
          if (isInitiallyEmpty)<br>
          {<br>
            IClasspathEntry jreClasspathEntry =<br>
              JavaCore.newVariableEntry<br>
                (new Path(JavaRuntime.JRELIB_VARIABLE), new
Path(JavaRuntime.JRESRC_VARIABLE), new
Path(JavaRuntime.JRESRCROOT_VARIABLE));<br>
            for (Iterator&lt;IClasspathEntry&gt; i =
classpathEntries.iterator(); i.hasNext(); )<br>
            {<br>
              IClasspathEntry classpathEntry = i.next();<br>
              if
(classpathEntry.getPath().isPrefixOf(jreClasspathEntry.getPath()))<br>
              {<br>
                i.remove();<br>
              }<br>
            }<br>
  <br>
            String jreContainer = JavaRuntime.JRE_CONTAINER;<br>
            String complianceLevel =
CodeGenUtil.EclipseUtil.getJavaComplianceLevel(project);<br>
            if ("1.5".equals(complianceLevel))<br>
            {<br>
              jreContainer +=
"/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5";<br>
            }<br>
            else if ("1.6".equals(complianceLevel))<br>
            {<br>
              jreContainer +=
"/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6";<br>
            }<br>
            classpathEntries.add(JavaCore.newContainerEntry(new
Path(jreContainer)));<br>
          }<br>
  <br>
          if ((style &amp; EMF_EMPTY_PROJECT_STYLE) == 0)<br>
          {<br>
            if ((style &amp; EMF_PLUGIN_PROJECT_STYLE) != 0)<br>
            {<br>
              classpathEntries.add(JavaCore.newContainerEntry(new
Path("org.eclipse.pde.core.requiredPlugins")));<br>
<br>
              if ((style &amp; EMF_GWT_PROJECT_STYLE) != 0)<br>
              {<br>
               
classpathEntries.add(JavaCore.newContainerEntry(new
Path("com.google.gwt.eclipse.core.GWT_CONTAINER")));<br>
                if ((style &amp; EMF_EDITOR_PROJECT_STYLE) != 0)<br>
                {<br>
                 
classpathEntries.add(JavaCore.newContainerEntry(new
Path("com.google.appengine.eclipse.core.GAE_CONTAINER")));<br>
                }<br>
              }<br>
  <br>
              // Remove variables since the plugin.xml should
provide the complete path information.<br>
              //<br>
              for (Iterator&lt;IClasspathEntry&gt; i =
classpathEntries.iterator(); i.hasNext(); )<br>
              {<br>
                IClasspathEntry classpathEntry = i.next();<br>
                if (classpathEntry.getEntryKind() ==
IClasspathEntry.CPE_VARIABLE &amp;&amp; <br>
                     
!JavaRuntime.JRELIB_VARIABLE.equals(classpathEntry.getPath().toString())
||<br>
                      classpathEntry.getEntryKind() ==
IClasspathEntry.CPE_PROJECT)<br>
                {<br>
                  i.remove();<br>
                }<br>
              }<br>
            }<br>
            else<br>
            {<br>
             
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_CORE_RUNTIME", "org.eclipse.core.runtime");<br>
             
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_CORE_RESOURCES", "org.eclipse.core.resources");<br>
             
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_COMMON", "org.eclipse.emf.common");<br>
             
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_ECORE", "org.eclipse.emf.ecore");<br>
  <br>
              if ((style &amp; EMF_XML_PROJECT_STYLE) != 0)<br>
              {<br>
               
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_ECORE_XMI", "org.eclipse.emf.ecore.xmi");<br>
              }<br>
  <br>
              if ((style &amp; EMF_MODEL_PROJECT_STYLE) == 0)<br>
              {<br>
               
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_EDIT", "org.eclipse.emf.edit");<br>
  <br>
                if ((style &amp; EMF_EDIT_PROJECT_STYLE) == 0)<br>
                {<br>
                 
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_SWT", "org.eclipse.swt");<br>
                 
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_JFACE", "org.eclipse.jface");<br>
                 
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_UI_VIEWS", "org.eclipse.ui.views");<br>
                 
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_UI_EDITORS", "org.eclipse.ui.editors");<br>
                 
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_UI_IDE", "org.eclipse.ui.ide");<br>
                 
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"ECLIPSE_UI_WORKBENCH", "org.eclipse.ui.workbench");<br>
                 
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_COMMON_UI", "org.eclipse.emf.common.ui");<br>
                 
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_EDIT_UI", "org.eclipse.emf.edit.ui");<br>
                  if ((style &amp; EMF_XML_PROJECT_STYLE) == 0)<br>
                  {<br>
                   
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"EMF_ECORE_XMI", "org.eclipse.emf.ecore.xmi");<br>
                  }<br>
                }<br>
              }<br>
  <br>
              if ((style &amp; EMF_TESTS_PROJECT_STYLE) != 0)<br>
              {<br>
               
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
"JUNIT", "org.junit");<br>
              }<br>
<br>
              if (pluginVariables != null)<br>
              {<br>
                for (Iterator&lt;?&gt; i =
pluginVariables.iterator(); i.hasNext(); )<br>
                {<br>
                  Object variable = i.next();<br>
                  if (variable instanceof IClasspathEntry)<br>
                  {<br>
                   
classpathEntries.add((IClasspathEntry)variable);<br>
                  }<br>
                  else if (variable instanceof String)<br>
                  {<br>
                    String pluginVariable = (String)variable;<br>
                    String name;<br>
                    String id;<br>
                    int index = pluginVariable.indexOf("=");<br>
                    if (index == -1)<br>
                    {<br>
                      name =
pluginVariable.replace('.','_').toUpperCase();<br>
                      id = pluginVariable;<br>
                    }<br>
                    else<br>
                    {<br>
                      name = pluginVariable.substring(0, index);<br>
                      id = pluginVariable.substring(index + 1);<br>
                    }<br>
                   
CodeGenUtil.EclipseUtil.addClasspathEntries(classpathEntries,
name, id);<br>
                  }<br>
                }<br>
              }<br>
            }<br>
          }<br>
  <br>
          javaProject.setRawClasspath<br>
            (classpathEntries.toArray(new
IClasspathEntry[classpathEntries.size()]),<br>
             new SubProgressMonitor(progressMonitor, 1));<br>
        }<br>
  <br>
        if (isInitiallyEmpty)<br>
        {<br>
          javaProject.setOutputLocation<br>
            (new Path("/" + javaSource.segment(0) + (((style &amp;
EMF_GWT_PROJECT_STYLE) != 0) &amp;&amp; ((style &amp;
EMF_EDITOR_PROJECT_STYLE) != 0) ? "/war/WEB-INF/classes" :
"/bin")), <br>
             new SubProgressMonitor(progressMonitor, 1));<br>
        }<br>
      }<br>
      catch (Exception exception)<br>
      {<br>
        exception.printStackTrace();<br>
        CodeGenEcorePlugin.INSTANCE.log(exception);<br>
      }<br>
      finally<br>
      {<br>
        progressMonitor.done();<br>
      }<br>
  <br>
      return project;<br>
    }<br>
    <br>
    public static String findOrCreateContainerHelper<br>
      (String rootLocation, String encodedPath, Monitor
progressMonitor) throws CoreException<br>
    {<br>
      int index = encodedPath.indexOf("/./");<br>
      if (encodedPath.endsWith("/.") &amp;&amp; index != -1)<br>
      {<br>
        IPath modelProjectLocation = new
Path(encodedPath.substring(0, index));<br>
        IPath fragmentPath = new Path(encodedPath.substring(index
+ 3, encodedPath.length() - 2));<br>
  <br>
        IPath projectRelativePath =  new
Path(modelProjectLocation.lastSegment()).append(fragmentPath);<br>
  <br>
        CodeGenUtil.EclipseUtil.findOrCreateContainer<br>
          (projectRelativePath,<br>
           true,<br>
           modelProjectLocation,<br>
           //DMS Why not this?<br>
           //new SubProgressMonitor(progressMonitor, 1));<br>
          
BasicMonitor.toIProgressMonitor(CodeGenUtil.createMonitor(progressMonitor,
1)));<br>
  <br>
        return projectRelativePath.makeAbsolute().toString();<br>
      }<br>
      else if (rootLocation != null)<br>
      {<br>
        // Look for a likely plugin name.<br>
        //<br>
        index = encodedPath.indexOf("/org.");<br>
        if (index == -1)<br>
        {<br>
          index = encodedPath.indexOf("/com.");<br>
        }<br>
        if (index == -1)<br>
        {<br>
          index = encodedPath.indexOf("/javax.");<br>
        }<br>
        if (index != -1)<br>
        {<br>
          IPath projectRelativePath = new
Path(encodedPath.substring(index, encodedPath.length()));<br>
          index = encodedPath.indexOf("/", index + 5);<br>
          if (index != -1)<br>
          {<br>
            IPath modelProjectLocation = new Path(rootLocation +
"/" + encodedPath.substring(0, index));<br>
    <br>
            CodeGenUtil.EclipseUtil.findOrCreateContainer<br>
              (projectRelativePath,<br>
               true, <br>
               modelProjectLocation, <br>
               //DMS Why not this?<br>
               //new SubProgressMonitor(progressMonitor, 1));<br>
              
BasicMonitor.toIProgressMonitor(CodeGenUtil.createMonitor(progressMonitor,
1)));<br>
    <br>
            return projectRelativePath.makeAbsolute().toString();<br>
          }<br>
        }<br>
      }<br>
  <br>
      return encodedPath;<br>
    }<br>
  }<br>
}</small><br>
<br>
<br>
<br>
On 07/09/2011 8:31 AM, Alex wrote:
<blockquote cite="mid:j4823p$cnn$1@news.eclipse.org" type="cite">Hello,
<br>
<br>
In our application there are projects that are created by users,
these projects do not have builders associated with them. In next
release we added builders. This change will be okay for newly
created projects, but existing projects don't have builders
defined in .project file, thus builders do not work for these
projects.
<br>
<br>
One way of solving this is to examine each project's .project file
and see if builder entry exists, if not - add it. Is there a
better way to do this, as it sounds like a pretty common issue
that might arise in Eclipse application.
<br>
<br>
Thanks,
<br>
Alex
<br>
</blockquote>
</body>
</html>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Introducing builder for existing projects [message #723123 is a reply to message #723103] Wed, 07 September 2011 17:34 Go to previous message
Alex Kravets is currently offline Alex KravetsFriend
Messages: 546
Registered: November 2009
Senior Member
Thanks Ed,

Yes, I meant to use API. By the way, something is wrong with formatting of you reply.

Alex
Previous Topic:getIndentPrefixes and getIndentPrefixesForTab return values
Next Topic:How to programmatically register a view
Goto Forum:
  


Current Time: Sat Sep 19 13:53:07 GMT 2020

Powered by FUDForum. Page generated in 0.01665 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top