xolstice / protobuf-maven-plugin

Maven Plugin that executes the Protocol Buffers (protoc) compiler
https://www.xolstice.org/protobuf-maven-plugin/
Other
236 stars 78 forks source link

Generated source files from 'compile-custom' are not getting added to the class path. #63

Closed cielo closed 5 years ago

cielo commented 5 years ago

Describe the bug I am using compile goal to generate main java source files, as well as compile-custom goal to run 'grpc-java' plugin to generate 'grpc-java' source files.

The goal 'compile' generates files in target/generated-sources/protobuf/java

while the goal 'compile-custom' generates files in target/generated-sources/protobuf/grpc-java

This seems to be the correct behavior as defined in the document and I am fine with this part.

The problem that I am facing is... While my IDE correctly adds generated source files of compile goal to classpath, it does not add generated source files of compile-custom to the classpath. When I inspect .classpath file in the root directory, I am only seeing target/generated-sources/protobuf/java but not the one with target/generated-sources/protobuf/grpc-java.

The workaround method that I am using is to force all source files (from compile & compile-custom) to be generated in a predefined directory using outputDirectory & clearOutputDirectory configurations.

  ...
  <outputDirectory>${basedir}/target/generated-sources/protobuf/java</outputDirectory>
  <clearOutputDirectory>false</clearOutputDirectory>
  ...

While this works, I have to explicitly define /target/generated-sources/protobuf/java/... and I want to avoid it. I would rather rely on default behavior, if the default behavior can correctly adds result of `compile-custom' to classpaths.

Is this an expected behavior?

Environment

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T08:41:47-08:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_74, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_74.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.5", arch: "x86_64", family: "mac"

Standalone or IDE Are you are running Maven inside an IDE, and in that case, which IDE? Yes, I am using Maven inside IDE. Visual Code Studio 2

Plugin configuration

        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <version>1.21.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>1.21.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>1.21.0</version>
        </dependency>

            ....
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.21.0:exe:${os.detected.classifier}</pluginArtifact>
                    <outputDirectory>${basedir}/target/generated-sources/protobuf/java</outputDirectory>
                    <clearOutputDirectory>false</clearOutputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

To Reproduce Steps to reproduce the behavior:

  1. Execute compile & compile-custom goals in Visual Code Studio 2.
  2. Watch IDE throwing unknown classes for Grpc related source files, while it can correctly interpret non-Grpc java source files.

Expected behavior Just like how result directory of compile goal gets added to the classpath, I want the result directory of compile-custom goal to be added to the class path as well by default without any configuration changes.

sergei-ivanov commented 5 years ago

Hi, Are you missing compile-custom in the list of goals in the POM? Maybe the IDE does not read it from the POM and does not know that you have an additional output. I suggest adding compile-custom as a separate execution and removing the outputDirectory override, then everything will hopefully be resolved correctly. If that does not work, then it may be a problem with the IDE, in which case you may want to raise an issue with the IDE developers instead.

Try this:

            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.21.0:exe:${os.detected.classifier}</pluginArtifact>
                    <clearOutputDirectory>false</clearOutputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>protobuf-compile</id>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>protobuf-compile-custom</id>
                        <goals>
                            <goal>compile-custom</goal>
                            <goal>test-compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
cielo commented 5 years ago

Hello,

Thanks for the reply. Your suggestion solved my problem!

I somehow have been mis-reading test-compile goal in my old version of pom.xml as compile-custom until I saw your response. duh...

After following your approach, everything is working as expected now. Honestly, I am not sure how test-compile ended up in there, as example in grpc-java doc has compile-custom, and not a single line of test-compile

Also, I guess I will have to study maven little bit more.. as I had not known that having goals explicitly listed in execution allows output directory to be added to the class path. (is it true?)

Thank you so much!

sergei-ivanov commented 5 years ago

Hi,

I am glad your issue has been resolved. I still recommend configuring separate executions for compile and compile-custom, even if you drop the "test" goals.

If you do not list all goals in the POM, the IDE will have no knowledge of the actual configuration. IntelliJ IDEA, for example, runs generate-sources lifecycle goal against the POM to figure out the directories with generated sources. I reckon that VCS2 may be doing the same thing.