Krishnanunni P V
Krishnanunni P V

Reputation: 699

Error with import in .proto file

I have a MyResponse.proto file which imports two .proto files Alternative.proto and Index.proto.

MyResponse.proto file:

import "Alternative.proto";
import "Index.proto";
message MyResponse {
  repeated AlternativeV1 alternativeV1 = 1;
  required IndexV1 indexV1 = 2;
}

Alternative.proto file:

import "Reference.proto";
message AlternativeV1 {
  required string name = 1;
  required string id = 2;
}

Index.proto file:

message IndexV1 {
  required string name = 1;
}

I'm getting the error "AlternativeV1" is not defined while compiling the proto file using maven. I'm using Intellij as IDE. The types AlternativeV1 and IndexV1 are shown as 'unresolved reference' in IDE. Can anyone help me in fixing this issue ?

Upvotes: 1

Views: 6007

Answers (1)

SubOptimal
SubOptimal

Reputation: 22973

Find below a small working example. So you can compare it with your project settings.

Assume following file and directory structure.

pom.xml
src/main/proto/Alternative.proto
src/main/proto/Index.proto
src/main/proto/MyResponse.proto

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sub.optimal</groupId>
    <artifactId>ProtoExample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>2.6.1</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>-->
    </properties>

    <pluginRepositories>
        <pluginRepository>
            <releases>
                <updatePolicy>never</updatePolicy>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo.maven.apache.org/maven2</url>
        </pluginRepository>
        <pluginRepository>
            <id>protoc-plugin</id>
            <url>https://dl.bintray.com/sergei-ivanov/maven/</url>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <plugins>
            <plugin>
                <groupId>com.google.protobuf.tools</groupId>
                <artifactId>maven-protoc-plugin</artifactId>
                <version>0.4.4</version>
                <configuration>
                    <protocExecutable>protoc</protocExecutable>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Alternative.proto

package sub.optimal;
message AlternativeV1 {
    required string name = 1;
    required string id = 2;
}

Index.proto

package sub.optimal;
message IndexV1 {
    required string name = 1;
}

MyResponse.proto

package sub.optimal;
option java_outer_classname = "MyResponseProtos";
import "Alternative.proto";
import "Index.proto";
message MyResponse {
    repeated AlternativeV1 alternativeV1 = 1;
    required IndexV1 indexV1 = 2;
}

The following has been added to the *.proto files

  • package - to generate valid Java source
  • option java_outer_classname - otherwise the protoc compiler would generate a default class MyResponseOuterClass.java

Running mvn compile will generate the classes below target/classes/sub/optimal/.

Upvotes: 1

Related Questions