Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiling Generated .java Into .class Files #92

Closed
pbtura opened this issue Jan 25, 2021 · 6 comments
Closed

Compiling Generated .java Into .class Files #92

pbtura opened this issue Jan 25, 2021 · 6 comments

Comments

@pbtura
Copy link

pbtura commented Jan 25, 2021

I'm seeing some strange behavior when trying to use this plugin and I'm not sure if it is a bug or if I am just doing something wrong. When I run a maven build, the generated .java files are being created as expected but there are no corresponding .class files. The project is getting packaged into a library jar, so if the class files are missing, other projects that use the jar will not be able to use the generated files. What do I need to do to make sure the class files get created?

Here is what my pom looks like currently:

   <plugin>
   	    <groupId>org.bsc.maven</groupId>
   	    <artifactId>maven-processor-plugin</artifactId>
   	   <version>4.5-jdk8</version>
   	    <executions>
   	        <execution>
   	            <id>process</id>
   	            <goals>
   	                <goal>process</goal>
   	            </goals>
   	            <phase>generate-sources</phase>
   	            <configuration>
   	            	<outputDirectory>${project.build.directory}/generated-sources/annotations</outputDirectory>
   	            	<outputClassDirectory>${project.build.directory}/generated-sources/classes</outputClassDirectory>
   	            	<addOutputDirectoryToCompilationSources>true</addOutputDirectoryToCompilationSources>
   	                <processors>
   	                    <processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
   	                </processors>
   	                <overwrite>true</overwrite>
   	            </configuration>
   	        </execution>
   	    </executions>
   	    <dependencies>
   	        <dependency>
   	            <groupId>org.hibernate</groupId>
   	            <artifactId>hibernate-jpamodelgen</artifactId>
   	            <version>5.3.13.Final</version>		            
   	        </dependency>		       
   	    </dependencies>
   	</plugin>
   	
   	<plugin>
       	<groupId>org.apache.maven.plugins</groupId>
   	    <artifactId>maven-compiler-plugin</artifactId>		    	
   	    <executions>
   	          <execution>
   	            <id>generate-annotated</id>
   	            <phase>generate-sources</phase>
   	            <goals>
   	              <goal>compile</goal>
   	            </goals>
   	            <configuration>
   			       <compilerArgument>-proc:none</compilerArgument>
   			    </configuration>
   	          </execution>
   		</executions>
   	</plugin>		
   	<plugin>
   	        <groupId>org.codehaus.mojo</groupId>
   	        <artifactId>build-helper-maven-plugin</artifactId>	        
   	        <executions>
   	          <execution>
   	            <id>add-source</id>
   	            <phase>generate-sources</phase>
   	            <goals>
   	              <goal>add-source</goal>
   	            </goals>
   	            <configuration> 
   	              <sources>	              
   	                <source>${project.build.directory}/generated-sources/annotations</source>	               
   	              </sources>	            
   	            </configuration>
   	          </execution>
   	        </executions>
   	</plugin>

When I run 'mvn clean package' everything passes and I see the generated sources in generated-sources/annotations. When I look at generated-sources/classes however the directory is empty. The resulting jar also lacks any of the generated class files.

@bsorrentino
Copy link
Owner

Hi @pbtura let me investigate

@bsorrentino bsorrentino self-assigned this Jan 26, 2021
@pbtura
Copy link
Author

pbtura commented Jan 26, 2021

I spent some time experimenting with a test project to see if I could isolate the issue. I made two discoveries that might be helpful. First, the <outputClassDirectory> tag does not seem to work. It will create a directory with that name but no files will ever be placed in it. Second, and more important, the <outputDirectory> tag seems to be the source of the problem. I changed the value of that tag from

${project.build.directory}/generated-sources/annotations

to

${project.build.directory}/generated-sources/java

I re-ran the build and the .class files for the generated source were created in the ${project.build.directory}/classes directory.

${project.build.directory}/generated-sources/annotations is the default output location when doing annotation processing via the maven-compiler-plugin. I wonder if there is some sort of conflict, even though the proc argument is set to none?

@bsorrentino
Copy link
Owner

Hi @pbtura thanks for help, I’ll focus on your findings

bsorrentino added a commit that referenced this issue Jan 29, 2021
@bsorrentino
Copy link
Owner

Hi @pbtura

in this branch I've performed a test to replicate your issue

I haven't been able to replicate the error and in my test all seems work well. Take a look to the branch. Below a summary of plugin's configuration

    <plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <executions>
            <execution>

                <id>process</id>
                <goals>
                    <goal>process</goal>
                </goals>
                <phase>process-sources</phase>
                <configuration>
                    <addOutputDirectoryToCompilationSources>true</addOutputDirectoryToCompilationSources>
                    <failOnError>false</failOnError>
                    <processors>
                        <processor>org.bsc.maven.plugin.processor.test.TestGenerateSourceProcessor</processor>
                    </processors>
                </configuration>

            </execution>
        </executions>
    </plugin>

Let me know

@pbtura
Copy link
Author

pbtura commented Jan 29, 2021

Ok, I will check that out when I get some time. In the mean time, I posted my sample project to github if you want to try running some tests against it. The project is located here: https://github.com/pbtura/jpa-test
If you do 'mvn clean package -P broken' you should see that the java sources get generated but the corresponding class files for some of them such as Country_ and StateProvince_ will be missing.

@bsorrentino
Copy link
Owner

Hi @pbtura I've cloned your project jpa-test. I've applied the following update to the pom.xml

  1. Removed profiles
  2. Removed <outputDirectory> from plugin configuration
  3. Removed build-helper-maven-plugin

And all worked as expected. Take note that the default plugin output path is ${project.build.directory}/generated-sources/apt

Below the updated pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.tura</groupId>
	<artifactId>jpa-test</artifactId>
	<version>0.0.1</version>
	<packaging>jar</packaging>

	<properties>
		<classes.generated.path>${project.build.directory}/generated-sources/annotations</classes.generated.path>
	</properties>

	<dependencies>
		<dependency>
			<groupId>jakarta.persistence</groupId>
			<artifactId>jakarta.persistence-api</artifactId>
			<version>2.2.3</version>
		</dependency>
		<dependency>
			<groupId>jakarta.validation</groupId>
			<artifactId>jakarta.validation-api</artifactId>
			<version>2.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-jpamodelgen</artifactId>
			<version>5.3.13.Final</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.3.13.Final</version>

		</dependency>
		<dependency>
			<groupId>org.jboss.spec.javax.ejb</groupId>
			<artifactId>jboss-ejb-api_3.2_spec</artifactId>
			<version>2.0.0.Final</version>
		</dependency>

		<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy</artifactId>
			<version>3.0.7</version>
		</dependency>

		<dependency>
			<groupId>org.codehaus.groovy</groupId>
			<artifactId>groovy-sql</artifactId>
			<version>3.0.7</version>
		</dependency>

	</dependencies>

	<build>

		<pluginManagement>
			<plugins>

				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-ejb-plugin</artifactId>
					<version>3.0.1</version>
					<configuration>
						<!-- Tell Maven we are using EJB -->
						<ejbVersion>3.2</ejbVersion>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
		<plugins>


			<plugin>
				<groupId>org.bsc.maven</groupId>
				<artifactId>maven-processor-plugin</artifactId>
				<version>4.5-jdk8</version>
				<executions>
					<execution>
						<id>process</id>
						<goals>
							<goal>process</goal>
						</goals>
						<phase>generate-sources</phase>
						<configuration>
							<includes>
								<include>com/tura/jpa/domain/*.java</include>
							</includes>
						<addOutputDirectoryToCompilationSources>true</addOutputDirectoryToCompilationSources>
							<processors>
								<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
							</processors>
						</configuration>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>org.hibernate</groupId>
						<artifactId>hibernate-jpamodelgen</artifactId>
						<version>5.3.13.Final</version>
					</dependency>
				</dependencies>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<proc>none</proc>

					<verbose>true</verbose>

				</configuration>

			</plugin>

		</plugins>
	</build>

</project>
``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants