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

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

Closed
jervin opened this Issue Feb 23, 2016 · 13 comments

Comments

3 participants
@jervin
Copy link

jervin commented Feb 23, 2016

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

@jvstein

This comment has been minimized.

Copy link

jvstein commented Feb 23, 2016

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 sergei-ivanov self-assigned this Feb 23, 2016

@sergei-ivanov

This comment has been minimized.

Copy link
Member

sergei-ivanov commented Feb 26, 2016

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

This comment has been minimized.

Copy link

jervin commented Mar 2, 2016

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

This comment has been minimized.

Copy link
Member

sergei-ivanov commented Mar 2, 2016

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

This comment has been minimized.

Copy link

jervin commented Mar 2, 2016

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

@jervin jervin closed this Mar 2, 2016

@sergei-ivanov

This comment has been minimized.

Copy link
Member

sergei-ivanov commented Mar 2, 2016

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

This comment has been minimized.

Copy link
Member

sergei-ivanov commented Mar 3, 2016

@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.

@sergei-ivanov sergei-ivanov reopened this Mar 3, 2016

@jervin

This comment has been minimized.

Copy link

jervin commented Mar 4, 2016

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

This comment has been minimized.

Copy link

jvstein commented Mar 4, 2016

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

This comment has been minimized.

Copy link
Member

sergei-ivanov commented Mar 4, 2016

@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

This comment has been minimized.

Copy link
Member

sergei-ivanov commented Mar 4, 2016

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

This comment has been minimized.

Copy link

jervin commented Mar 8, 2016

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

This comment has been minimized.

Copy link
Member

sergei-ivanov commented May 17, 2016

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.

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