Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to trigger 'organize imports' from Ant build file
How to trigger 'organize imports' from Ant build file [message #1252545] Fri, 21 February 2014 10:28 Go to next message
Till F. is currently offline Till F.Friend
Messages: 58
Registered: August 2012
Member
Hi,

thread title says it all.

I posted in the EMF topic, because there dosn't seem to be one for Ant-specific questions, and the context is EMF-specific in my case:

I'm using an Ant build file to automatically generate code for all .genmodel files in a project. Unfortunately, EMF resists on generating certain unused imports every time. I'd like to get rid of those automatically. Obovious solution would be to trigger "organize imports" after build. Is it possible to do this from an Ant build file?

Same question here (very old, unanswered): http://www.eclipse.org/forums/index.php/t/75949/


/edit:
Feel free to move into another topic...

[Updated on: Fri, 21 February 2014 10:47]

Report message to a moderator

Re: How to trigger 'organize imports' from Ant build file [message #1252628 is a reply to message #1252545] Fri, 21 February 2014 12:23 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Till,

Comments below.

On 21/02/2014 11:28 AM, Till Fischer wrote:
> Hi,
>
> thread title says it all.
>
> I'm using an Ant build file to automatically generate code for all
> .genmodel files in a project. Unfortunately, EMF resists on generating
> certain unused imports every time.
One approach there is to comment those out. A commented out import
blocks that import from being merged during generation...
> I'd like to get rid of those automatically. Obovious solution would be
> to trigger "organize imports" after build. Is it possible to do this
> from an Ant build file?
I have no idea how you'd trigger other Eclipse actions. That more a
general Eclipse question. I have been investigating being able to apply
project clean actions on all the projects into which files have been
generated, so that would help, but isn't there yet.
>
> Same question here (very old, unanswered):
> http://www.eclipse.org/forums/index.php/t/75949/


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to trigger 'organize imports' from Ant build file [message #1252767 is a reply to message #1252628] Fri, 21 February 2014 15:33 Go to previous messageGo to next message
Till F. is currently offline Till F.Friend
Messages: 58
Registered: August 2012
Member
Quote:
One approach there is to comment those out. A commented out import
blocks that import from being merged during generation...


First thought this would be the trick, but unforunately it would also force me to check-in those files :s

Thus, I will continue using Ctrl+Shift+O
Re: How to trigger 'organize imports' from Ant build file [message #1252844 is a reply to message #1252767] Fri, 21 February 2014 17:00 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Till,

I'm not aware that EMF ever generates unused imports. I expect
something must have been modified in those files. If you delete all the
generated files and regenerate them, are there unused imports?

On 21/02/2014 4:33 PM, Till Fischer wrote:
> Quote:
>> One approach there is to comment those out. A commented out import
>> blocks that import from being merged during generation...
>
>
> First thought this would be the trick, but unforunately it would also
> force me to check-in those files :s
>
> Thus, I will continue using Ctrl+Shift+O


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to trigger 'organize imports' from Ant build file [message #1255629 is a reply to message #1252844] Mon, 24 February 2014 16:59 Go to previous messageGo to next message
Till F. is currently offline Till F.Friend
Messages: 58
Registered: August 2012
Member
Yep, there are unused imports being generated when I delete an re-generate the file. With a previous EMF version those imports were not generated, thus I already wondered about them. They either appeared after updating from 2.9.0 to 2.9.1 or from 2.9.1 to 2.9.2.

I thougt that it could be related to the fact that I don't follow the "regular" methodology to have different plugins/projects for model and edit code. Instead, I generate everything into a common project but different source folders (src-model, src-edit) are used. I do this because I partitioned my model into several .ecore files while there are still cross-references in between. Having different projects would cause circular dependencies (and is pretty anoying anyway).

I just investigated this further. The "unused" imports are always wildcard-imports. I also found strange looking import artefacts like the following in several places. This one comes from the generated MyModelSwitch<T> class. It looks like EMF normally shouldn't generate wildcard imports at all:

package de.my.company.MyModel.util;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.util.Switch;

import de.my.company.MyModel.*;
import de.my.company.MyModel.ElementOne;
import de.my.company.MyModel.ElementTwo;
import de.my.company.MyModel.AnotherElement;
import de.my.company.MyModel.Foo;
import de.my.company.MyModel.Bar;


The useless import is easy to spot Smile

[Updated on: Mon, 24 February 2014 17:01]

Report message to a moderator

Re: How to trigger 'organize imports' from Ant build file [message #1255655 is a reply to message #1255629] Mon, 24 February 2014 17:33 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Till,

Comments below.

On 24/02/2014 5:59 PM, Till Fischer wrote:
> Yep, there are unused imports being generated when I delete an
> re-generate the file. With a previous EMF version those imports were
> not generated, thus I already wondered about them. They either
> appeared after updating from 2.9.0 to 2.9.1 or from 2.9.1 to 2.9.2.
That seems unlikely because not much happened in those streams...
>
> I thougt that it could be related to the fact that I don't follow the
> "regular" methodology to have different plugins/projects for model and
> edit code. Instead, I generate everything into a common project but
> different source folders (src-model, src-edit) are used. I do this
> because I partitioned my model into several .ecore files while there
> are still cross-references in between. Having different projects would
> cause circular dependencies (and is pretty anoying anyway).
No, that should be okay.
>
> I just investigated this further. The "unused" imports are always
> wildcard-imports.
That's kind of fishy. Because EMF (almost) always generates that .*
import for such cases (except when there are collisions with java.lang
names). But I expect that invoking organize imports converts them to
the latter, and then generating again reintroduces them again.
> I also found strange looking import artefacts like the following in
> several places. This one comes from the generated MyModelSwitch<T>
> class. It looks like EMF normally shouldn't generate wildcard imports
> at all:
No, it normally does. Note that I recently fixed
https://bugs.eclipse.org/bugs/show_bug.cgi?id=427668 which is kind of
related (but has to do with the special case of no java.lang conflicts
before generating and then introducing them and regenerating). Do you
have the "Import Organizing" property set to true in the GenModel; that
should typically clean up unused imports. Also, I just committed the
changes for https://bugs.eclipse.org/bugs/show_bug.cgi?id=428935 which
allows you to specify that general cleanup actions should be applied
(and that can include things like organizing imports); I'm not sure it
works from Ant though. Speaking of which, if you defined your model with
Xcore, you'd get automatic build on save without an Ant action and it's
very easy to convert your existing GenModel to Xcore:
https://wiki.eclipse.org/Xcore
>
>
> package de.my.company.MyModel.util;
>
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.emf.ecore.EPackage;
> import org.eclipse.emf.ecore.util.Switch;
>
> import de.my.company.MyModel.*;
> import de.my.company.MyModel.ElementOne;
> import de.my.company.MyModel.ElementTwo;
> import de.my.company.MyModel.AnotherElement;
> import de.my.company.MyModel.Foo;
> import de.my.company.MyModel.Bar;
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to trigger 'organize imports' from Ant build file [message #1258684 is a reply to message #1255655] Thu, 27 February 2014 16:40 Go to previous messageGo to next message
Till F. is currently offline Till F.Friend
Messages: 58
Registered: August 2012
Member
Ed Merks wrote on Mon, 24 February 2014 12:33

That seems unlikely because not much happened in those streams...
(...)
That's kind of fishy. Because EMF (almost) always generates that .*
import for such cases (except when there are collisions with java.lang
names). But I expect that invoking organize imports converts them to
the latter, and then generating again reintroduces them again.

Mh... not sure whether this is an explanation. Some of the generated sources are checked into SVN - without the .* import. But I cannot remember that I ever triggered "organize imports" on my generated sources before. And the problem also affects files that are generated from the scratch. Why does EMF prefer the .* notation? This doesn't seem to be robust to me.

Ed Merks wrote on Mon, 24 February 2014 12:33

Note that I recently fixed
https://bugs.eclipse.org/bugs/show_bug.cgi?id=427668 which is kind of
related (but has to do with the special case of no java.lang conflicts
before generating and then introducing them and regenerating). Do you
have the "Import Organizing" property set to true in the GenModel; that
should typically clean up unused imports.

I didn't know about this "Import Organizing" property of the genmodel. Activating it solved the problem. This should be the default for new models.

Regarding cross-references and circular dependencies:
They definitely occurred. Maybe I have to mention that in my .ecore and .genmodel files the other model files are *referenced* (and not included; indicated by a small arrow, e.g. in the EMF Generator editor).

For models A and B with elements a and b, this results in two Projects A and B, where the generated class a is only contained in A and b only in B. Im my case a imports b and b imports a. This can only compile if Project A has B as required project (on the classpath) and vice versa. Which is a circular dependency. Is it possible to avoid this? I mean, without changing the model?

Ed Merks wrote on Mon, 24 February 2014 12:33

Speaking of which, if you defined your model with
Xcore, you'd get automatic build on save without an Ant action and it's
very easy to convert your existing GenModel to Xcore:
https://wiki.eclipse.org/Xcore

I need Ant for easier source code deployment (full-build-with-a-single-click), not for automatic builds on resource save. But I think generation of all non-generated files would be triggered automatically with Xcore, as well. But for the moment I'm comfortable enough with EMF...
Re: How to trigger 'organize imports' from Ant build file [message #1258798 is a reply to message #1258684] Thu, 27 February 2014 19:21 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Till,

Comments below.

On 27/02/2014 5:40 PM, Till Fischer wrote:
> Ed Merks wrote on Mon, 24 February 2014 12:33
>> That seems unlikely because not much happened in those streams...
>> (...)
>> That's kind of fishy. Because EMF (almost) always generates that .*
>> import for such cases (except when there are collisions with
>> java.lang names). But I expect that invoking organize imports
>> converts them to the latter, and then generating again reintroduces
>> them again.
>
> Mh... not sure whether this is an explanation. Some of the generated
> sources are checked into SVN - without the .* import. But I cannot
> remember that I ever triggered "organize imports" on my generated
> sources before. And the problem also affects files that are generated
> from the scratch. Why does EMF prefer the .* notation?
Several of the files that are generated will generally end up importing
(pretty much) every generated interface, e.g., the XyzSwitch,
XyxAdapterFactory, XyzPackageImpl, and XyzFactoryImpl.
> This doesn't seem to be robust to me.
Not sure what you mean by robust....
>
> Ed Merks wrote on Mon, 24 February 2014 12:33
>> Note that I recently fixed
>> https://bugs.eclipse.org/bugs/show_bug.cgi?id=427668 which is kind of
>> related (but has to do with the special case of no java.lang
>> conflicts before generating and then introducing them and
>> regenerating). Do you have the "Import Organizing" property set to
>> true in the GenModel; that should typically clean up unused imports.
>
> I didn't know about this "Import Organizing" property of the genmodel.
> Activating it solved the problem. This should be the default for new
> models.
It is. But for existing *.genmodels it will be false.
>
> Regarding cross-references and circular dependencies:
> They definitely occurred. Maybe I have to mention that in my .ecore
> and .genmodel files the other model files are *referenced* (and not
> included; indicated by a small arrow, e.g. in the EMF Generator editor).
>
> For models A and B with elements a and b, this results in two Projects
> A and B, where the generated class a is only contained in A and b only
> in B. Im my case a imports b and b imports a. This can only compile if
> Project A has B as required project (on the classpath) and vice versa.
> Which is a circular dependency. Is it possible to avoid this? I mean,
> without changing the model?
It's okay to have circular dependencies across Ecore models but such
things should generally be managed by a single *.genmodel.
>
> Ed Merks wrote on Mon, 24 February 2014 12:33
>> Speaking of which, if you defined your model with Xcore, you'd get
>> automatic build on save without an Ant action and it's very easy to
>> convert your existing GenModel to Xcore: https://wiki.eclipse.org/Xcore
>
> I need Ant for easier source code deployment
> (full-build-with-a-single-click), not for automatic builds on resource
> save.
The behavior for how a *.xcore is automatically generated is no
different from how *.class files are automatically generated from *.java
files. I.e., it's just an automatic part of the build. Save a *.java
or *.xcore and the build kicks off, detects that the file is really
changed, and generated the *.class or *.java files...
> But I think generation of all non-generated files would be triggered
> automatically with Xcore, as well.
I'm not sure what this means... Xcore participates in the build
automatically; the *.java files it generates are automatically compiled
as well...
> But for the moment I'm comfortable enough with EMF...
Whatever makes you happy is best for you...


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:OclInEcore syntax to XMI for Ecore model
Next Topic:DynamicEStoreEObjectImpl intended usage
Goto Forum:
  


Current Time: Thu Apr 25 08:41:28 GMT 2024

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

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

Back to the top