Hellp Antonio,

Thanks for your extensive and detailed answer to my question.

After posing my question here, I did dig in a bit further and first searched for "Project" in the NB sources and then in their javadoc. One of the confusion in that searcg is that Apache Ant uses the term Project as reference to the content of a build.xml file. There I found the ProjectUtils and associated lookup. Ulitimately, this search did not get me an answer: the folder structure is not part of the project info accessible via ProjectUtils.

As a way to solve this I looked at GlobalPathRegistry.getDefault().getSourceRoots(), which does give a list of all possible source loactions in a project. With a filter on the name of the 'roots' in the source path to first contain the word "generated" and then have a prefix matching the location of the project I did find the location to write my generated files into. This is an Ant project so further testing was needed starting with maven.

At least, that worked when testing it  after opening my Ant based development project in a target NB instance. It fails in a freshly created project, both with Maven and Ant.  That it fails in such a project is clearly because nothing has been build yet and so the folder structure for build results, under the  'build' or 'target' folders, respectively for Ant and Maven did not exist.

A possible remedy seemed to be to add a java source file to these virgin projects and  then doe a build. Indeed, with this action, the build and target directory where created by the respective  build processes. However, the source path after the build was not changed: it did still not contain the now existing generated file folder. Closing and reopening the projects did not have any effect. (as did closing and reopening NetBeans).

So I got stuck.

I could go the brute force route by just peeking in the project folder and looking for build.xml or pom.xml eyc to determine the project type and then use the appropriate folder name.

Before going there I peeked into the NB source code at org.netbeans.modules.java.api.common.classpath.SourcePathImplementation.java to look for hints on how to handle this neatly. SourcePathImplementation control flow is generic with Ant specific operations determined by construction-time parameters, that's a start. To modify that code and get a nice solution with something like a static method in GlobalPathRegistry would be too much of a deviation for me.

Any suggestions on how not to go for the brute force approach?

Cheers
Eduard de Jong

antonio wrote on 23/06/2020 01:03:
Hi Eduard,

Now, this "how do I find from the code in my plugin what path to use for given project?" is a concise question and so is easier (and longer) to answer :-). I'm not very good at answering things, but let me have a try with this question.

== How do I find ...?

The "how do I find [...] from the code in my plugin" is an easy question.

In NetBeans the answer is usually the same independently of what you're looking for:

- Many NetBeans objects are also "Lookup.Provider"s [1], that give you a "Lookup" [2] to look inside them. - Once you have one object's lookup you just ask it for whatever you want to find.

Here's a tutorial for the Lookup API. I'm not sure if you need it, but I'm adding it just in case:

https://dzone.com/articles/netbeans-lookups-explained

== Projects

In your case you should have a "Project" [3] object somehow.

The "Project" objects represent projects in the IDE. Note that these "Project" objects implement the "Lookup.Provider" interface, so they return a Lookup object that you can use to find stuff in the project itself.

Of course there're many kinds of "Project" objects in NetBeans: you have Maven Projects [4], Ant Projects [5] (which in turn have many variants, including JavaEE Projects, for instance).

The good thing is that all these project types are just an implementation of the "Project" [3] interface, which in turn is a "Lookup.Provider" [1]. This means that you can "lookup for things" in all projects using the same interface.

== Project Sources

Now, right to the point of your question,

> My question now is: "how do I find from the code in my plugin what path
> to use for given project?" Is there, for instance, an SPI tat i could
> lookup to find it?

Yes, in fact there's a whole Project API to look things up in Projects!

The Project API is here: https://bits.netbeans.org/12.0/javadoc/org-netbeans-modules-projectapi/overview-summary.html

Note that there's also a Project SPI that you can use if you're building a Project of your own and want to add features to the Project.

And a possible answer to your question is in the "ProjectUtils" class in the API (that simplifies all Lookup for you), here:

http://bits.netbeans.org/dev/javadoc/org-netbeans-modules-projectapi/org/netbeans/api/project/ProjectUtils.html#getSources-org.netbeans.api.project.Project-

Sorry for the long answer. Feel free to ask any other questions you may have.

Kind regards,
Antonio


[1]
http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/Lookup.Provider.html

[2]
http://bits.netbeans.org/dev/javadoc/org-openide-util-lookup/org/openide/util/Lookup.html

[3]
http://bits.netbeans.org/dev/javadoc/org-netbeans-modules-projectapi/org/netbeans/api/project/Project.html

[4]
https://github.com/apache/netbeans/blob/master/java/maven/src/org/netbeans/modules/maven/NbMavenProjectImpl.java

[5]
http://netbeans.apache.org/tutorials/nbm-projecttypeant.html


El 21/6/20 a las 11:18, Eduard Karel de Jong escribió:
Hello Antonio,

Thanks for confirming that different types of projects in NB use different paths to store generated code.

My question now is: "how do I find from the code in my plugin what path to use for given project?" Is there, for instance, an SPI tat i could lookup to find it?

Or, maybe, it does not matter as NB combines all the possible paths in the various projects. In that case i could fix the location at, for instance "build/generated code," the path used in projects built with ant.

I tried google searches and they consistently misinterpret the question and go nowhere.
Cheers
Edaurd

antonio wrote on 20/06/2020 11:27:
Hi,

This depends on the build-system supporting the project type.

For instance, for Maven projects people usually leave generated sources in "target/generated-sources/whatever", while others use "src/main/whatever". For Maven projects you can customize these directories in your pom.xml.

AFAIK NetBeans correctly detects the generated sources folders and shows them in the IDE.

HTH,
Antonio


El 19/6/20 a las 17:20, Eduard Karel de Jong escribió:
Hi,

I'm working on a NB module that generates java code from files in software dev projects and  different types of projects have different locations for the generated files.

Is the location for generating code relevant and if so, is there a generic way to determine the proper location for a particular project type?

Thanks for any hint on how to find the answer to this question.

Cheers
eduard de jong


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@netbeans.apache.org
For additional commands, e-mail: dev-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists




---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@netbeans.apache.org
For additional commands, e-mail: dev-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists







Reply via email to