xolstice / protobuf-maven-plugin

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

Does this plugin have the option to generate C# code as well? #1

Closed jervin closed 5 years ago

jervin commented 8 years ago

I see in the usage page, python and C++. It would be helpful to me if this plugin could also handle C#.

jvstein commented 8 years ago

For what it's worth, C# support is currently only available in the 3.0 beta version of protoc. I think you should be able to use the compile-custom goal with that version, but I haven't tested it.

sergei-ivanov commented 8 years ago

Good call @jvstein, it actually works, I've just tested it. The configuration is as simple as follows:

            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile-custom</goal>
                        </goals>
                        <configuration>
                            <pluginId>csharp</pluginId>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

If there is enough popular demand, I'll add C# generation goals as first-class citizens to the future versions of the plugin. The problem is that there is already an explosion of goals, to handle all sorts of different outputs.

jervin commented 8 years ago

Awesome, is there a working repository to get the protobuf plugin? I tried https://api.bintray.com/maven/xolstice/maven/protobuf-maven-plugin and it seems to be dead.

sergei-ivanov commented 8 years ago

It is available directly from Maven Central now (i.e. no more tinkering with custom repos). I mainly use Bintray for artifact deployment and promotion to Maven Central, although, if you prefer, you can use their JCenter repo instead of Maven Central.

jervin commented 8 years ago

One more question, can I generate both the java and csharp classes from the same pom? Do I just add in a java ?

sergei-ivanov commented 8 years ago

Yes, you can. All you need to do is define an additional execution for the plugin to run compile-java goal. Two separate executions, each with its own configuration, is an important part of the set-up:

            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile-java</goal>
                        </goals>
                        <configuration>
                            <...>
                        </configuration>
                    </execution>
                    <execution>
                        <goals>
                            <goal>compile-custom</goal>
                        </goals>
                        <configuration>
                            <pluginId>csharp</pluginId>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
sergei-ivanov commented 8 years ago

@jervin I'll appreciate it if you let me know, whether you could successfully generate C# code with the suggested configuration. I am going to reopen this issue, because I think C# code generation deserves 1st class support from the plugin in the long run.

jervin commented 8 years ago

Hmm I did try what you suggested:

    <plugin>
        <groupId>org.xolstice.maven.plugins</groupId>
        <artifactId>protobuf-maven-plugin</artifactId>
        <version>0.5.0</version>
        <executions>
          <execution>
            <goals>
              <goal>compile-java</goal>
            </goals>
            <configuration>
                <protoSourceRoot>${project.basedir}/../../proto</protoSourceRoot>
                <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-1:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>protoc-gen-grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.9.0:exe:${os.detected.classifier}</pluginArtifact>
                <outputDirectory>${project.basedir}/src/main/java</outputDirectory>        
                <clearOutputDirectory>false</clearOutputDirectory>   
            </configuration>            
          </execution>
          <execution>
            <goals>
                <goal>compile-custom</goal>
            </goals>
            <configuration>
                <pluginId>csharp</pluginId>
            </configuration>
          </execution>
        </executions>
    </plugin>

But then I get the following when I run mvn clean install:

$ mvn clean install
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] 'build.plugins.plugin[org.xolstice.maven.plugins:protobuf-maven-plugin].executions.execution.id' must be unique but found duplicate execution with id default @ line 104, column 22
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-source-plugin is missing. @ line 62, column 15
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-javadoc-plugin is missing. @ line 74, column 15
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR]   The project studiolink.proto:protocol:0.0.1-SNAPSHOT (C:\work\svn\soundclear_studio\trunk\api\java\protocol\pom.xml) has 1 error
[ERROR]     'build.plugins.plugin[org.xolstice.maven.plugins:protobuf-maven-plugin].executions.execution.id' must be unique but found duplicate execution with id default @ line 104, column 22
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException

I wish I was better with Maven.

jvstein commented 8 years ago

Try specifying an id for each execution.

<plugin>
  <groupId>org.xolstice.maven.plugins</groupId>
  <artifactId>protobuf-maven-plugin</artifactId>
  <version>0.5.0</version>
  <executions>
    <execution>
      <id>java</id>
      <!-- snip -->     
    </execution>
    <execution>
      <id>csharp</id>
      <!-- snip -->
    </execution>
  </executions>
</plugin>
sergei-ivanov commented 8 years ago

@jervin That is not a problem, we are here to help. The error is pretty much self-explanatory: you need to add a unique <id>...</id> tag to each execution element:

    <plugin>
        <groupId>org.xolstice.maven.plugins</groupId>
        <artifactId>protobuf-maven-plugin</artifactId>
        <version>0.5.0</version>
        <executions>
          <execution>
            <id>protobuf-compile-java</id>
            <goals>
              <goal>compile-java</goal>
            </goals>
            <configuration>
                <protoSourceRoot>${project.basedir}/../../proto</protoSourceRoot>
                <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-1:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>protoc-gen-grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.9.0:exe:${os.detected.classifier}</pluginArtifact>
                <outputDirectory>${project.basedir}/src/main/java</outputDirectory>        
                <clearOutputDirectory>false</clearOutputDirectory>   
            </configuration>            
          </execution>
          <execution>
            <id>protobuf-compile-csharp</id>
            <goals>
                <goal>compile-custom</goal>
            </goals>
            <configuration>
                <pluginId>csharp</pluginId>
            </configuration>
          </execution>
        </executions>
    </plugin>

I am not sure why you are generating java files into src/main/java, which is pretty much against all maven guidelines, unless you are actually committing those sources into your VCS (which is again not good). By default, the plugin will generate java files into target/generated-sources/protobuf/java, and those sources will be compiled by java compiler and bundled into the resulting jar.

Therefore you may want to drop the following two lines:

<outputDirectory>${project.basedir}/src/main/java</outputDirectory>        
<clearOutputDirectory>false</clearOutputDirectory>   
sergei-ivanov commented 8 years ago

BTW, I have set up a gitter chat for this repo: Join the chat at https://gitter.im/xolstice/protobuf-maven-plugin It might be a better medium for discussing the configuration issues.

jervin commented 8 years ago

Yes it worked! I am having a bit of trouble getting the csharp and java classes generated at the same time, but this will probably suit my purposes to have two projects. Thanks alot for your help!

sergei-ivanov commented 8 years ago

Now that GitHub kind-of implements issue voting, please add votes (reactions to the headline comment) to help me gauge the demand for this feature. Please leave it open, I am going to address it in one of the future releaseso of the plugin.