netbeans-gradle-javaee-project icon indicating copy to clipboard operation
netbeans-gradle-javaee-project copied to clipboard

NetBeans cannot find the persistence.xml file

Open hildo opened this issue 11 years ago • 18 comments

Java source files using the @Entity annotations end up with a warning that the project does not contain a persistence unit. But it does

nb_screenshot

Need to figure out how to manage the project so the persistence.xml file can be found

hildo avatar Dec 02 '14 10:12 hildo

@kelemen You wouldn't happen to know who I can ask about this in the NetBeans core code? I presume that there is something pre-existing like the WebApp support, that the plugin needs to make the information available.

hildo avatar Dec 02 '14 11:12 hildo

I don't know who is responsible for this part. Maybe Geertjan knows this. Anyway, I usually just look up the error message (The project does not contain a persistance unit) in the properties files of NetBeans then search for the name of the error message in the Java sources to find what NB needs for something.

kelemen avatar Dec 02 '14 11:12 kelemen

Thanks for the help and guidance. I found the hint text, and found where it is used. It looks like the Project needs to have an instance of org.netbeans.modules.j2ee.persistence.spi.PersistenceScopesProvider in it's lookup. I will start looking into that.

hildo avatar Dec 05 '14 11:12 hildo

This probably needs to be implemented in a new Extension. It doesn't fit in the WebModuleExtensionDef, at least not right now. In my example, a standard Jar module defines the persistence context. While I think it's possible to define JPA contexts in Web modules, I won't focus on that now.

hildo avatar Dec 05 '14 12:12 hildo

@kelemen Can you see what I am doing wrong? I've committed my code, but I feel like I'm hitting a wall. I'm trying to define a new ExtensionDef, with associated model, to contain the logic for locating the persistence.xml (and providing a PersistenceScope).

But I don't seem to be able to create the model. I want to see what the Project has, so I'm trying to add log output in NbJpaModelBuilder.getProjectInfo, which I thought would be called (I tried to do the same things that seem to work with the NbWebModelBuilder. But I don't see the log output in the message.log file.

Is it not getting called? Or is it a logging util thing? I made the log level INFO, so I presumed it would be visible in messages.log. Can you see anything obvious I am not doing right? Thanks for any nudges in the right direction.

hildo avatar Dec 11 '14 10:12 hildo

I think NbJpaModelBuilder.getProjectInfo should be called. Though, it is called by the Gradle daemon (not within NetBeans), so I'm not sure what happens to your log (maybe it is somewhere in the log of the Gradle daemon).

kelemen avatar Dec 11 '14 10:12 kelemen

Oh, ok. That gets written to $HOME/.gradle/daemon, right? I'll keep digging away

hildo avatar Dec 11 '14 11:12 hildo

Ha! If I just write out to System.out instead of using a Logger, I can see it in the daemon log. Thanks for the help!

hildo avatar Dec 11 '14 11:12 hildo

@kelemen The next question. :) I'm trying to not hard-code the location of the persistence.xml, insofar as I want to check any of the known resource source directories and check for META-INF/persistence.xml from the root folder for any defined resource source directory.

So, I've attempted this by getting the sourceSets property from the project. Which returns an instance of org.gradle.api.internal.tasks.DefaultSourceSetContainer_Decorated

Now, I am inferring that this is an implementation of SourceSetContainer interface (http://www.gradle.org/docs/current/javadoc/org/gradle/api/tasks/SourceSetContainer.html). When I attempt to use this, however, it can't be found.

My guess is this is part of Gradle 2.0, and the version I'm using (via the transitive dependency to the Gradle NetBeans plugin) is 1.11.

I presume I have to use this older version, or at least to keep in sync with the Gradle plugin. If so, can you provide any examples of how to find the resources folders in the older API? Thanks for any help.

hildo avatar Dec 11 '14 12:12 hildo

Weird, if I look at the 1.11 javadoc, SourceSetContainer is there, but it's deprecated. ?

hildo avatar Dec 11 '14 12:12 hildo

Its there but kinda awkward to access because of ClassLoader issues. That is, you have to use reflection. See how I look up the sources in JavaSourcesModelBuilder. You may copy any inaccessible source code from my project if you need to.

kelemen avatar Dec 11 '14 13:12 kelemen

Thanks. I'll start looking at that.

Out of curiousity, if I copy those same classes from the model artifact into my own plugin, will there be a problem if I put them in the same package. It would be nice if the same code could be shared across the two plugins as an independant artifact, so I was thinking if I duplicate the code to keep them in the package in the hope that down the track we re-use the code properly. I'm hoping with the classloaders working as I think they work, it shouldn't be a problem. Just thought I'd ask up front, though.

hildo avatar Dec 12 '14 12:12 hildo

It might work since they are loaded by different class loaders but I wouldn't risk it. Later we can refactor these common code to a mini library anyway.

kelemen avatar Dec 12 '14 12:12 kelemen

@kelemen Is there a way to get the ClassPath from the Gradle plugin? The PersistenceScope needs to provide the persistenceXml as well as a ClassPath. I've done the first bit, and I'm hoping I can access the existing ClassPath off the Project. Can I get it from the Lookup? I presume that the Gradle plugin gets the classpath from the Tools API and converts it to the NetBeans ClassPath?

hildo avatar Dec 16 '14 12:12 hildo

I've tried using this

GradleClassPathProvider classPaths = jpaModule.getProject().getLookup().lookup(GradleClassPathProvider.class); But I get a compilation error

`` --- nbm-maven-plugin:3.14:manifest (default-manifest) @ netbeans-gradle-javaee-plugin ---

NBM Plugin generates manifest

BUILD FAILURE

Total time: 9.562 s Finished at: 2014-12-16T22:38:19+10:00

Final Memory: 24M/312M

Failed to execute goal org.codehaus.mojo:nbm-maven-plugin:3.14:manifest (default-manifest) on project netbeans-gradle-javaee-plugin: Module has friend dependency on org.netbeans.modules.java.preprocessorbridge but is not listed as a friend. -> [Help 1] ``

I'm not sure why

hildo avatar Dec 16 '14 12:12 hildo

GradleClassPathProvider is an internal class, you have to rely on ClassPathProvider. Also, there might be multiple instances of ClassPathProvider, so you have to get them all and see which one knows the class path (i.e., which one returns a non-null value). Though, I don't know what exactly PersistenceScope supposed to return.

kelemen avatar Dec 16 '14 12:12 kelemen

As I understand it (or think I do), the ClassPath represents the ClassPath which contains the classes for which the persistence.xml file represents. The context where this is being used is a Java class has an Entity annotation. The validation retrieves the available scopes and then finds a scope whose ClassPath contains the class being displayed in the editor and, if the ClassPath contains the class, checks for the existence of the named persistence.xml file (which is also provided by the scope). The validation fails if no scope can be found for the class.

Thanks for the help. I'll give that a go.

hildo avatar Dec 16 '14 19:12 hildo

I think I've got the changes we need, at least for a simple JAR module that defines its own persistence.xml. I still might see if I can get added as a friend dependency to the j2ee.persistenceapi module so this plugin doesn't have a reference to an explicit version.

hildo avatar Dec 20 '14 12:12 hildo