How to compile google-fhir proto files

2019-06-02 15:33发布

问题:

Looking at the readme for google-fhir it says to run bazel build, which works, however none of the protocol files have been compiled.

Running protoc --proto_path=. --java_out=. proto/stu3/resources.proto returns a bunch of error about other protos not being found. This goes backwards until 'descriptor.proto' which is not a proto in the folder. None of the protocol files in that directory will manually compile into java files.

*On another note, I was able to take other example protos from other sources and compile those successfully.

UPDATE

Running the command protoc --proto_path=proto/stu3/ --java_out=target proto/stu3/resources.proto from the google-fhir base directory returns the following output:

~/fhir-master$ protoc --proto_path=proto/stu3/ --java_out=target proto/stu3/resources.proto
proto/stu3/annotations.proto: File not found.
proto/stu3/codes.proto: File not found.
proto/stu3/datatypes.proto: File not found.
resources.proto: Import "proto/stu3/annotations.proto" was not found or had errors.
resources.proto: Import "proto/stu3/codes.proto" was not found or had errors.
resources.proto: Import "proto/stu3/datatypes.proto" was not found or had errors.
resources.proto:84:5: "String" is not defined.
resources.proto:87:14: "Extension" is not defined.
resources.proto:90:14: "Extension" is not defined.
resources.proto:94:5: "Reference" is not defined.
resources.proto:97:5: "PositiveInt" is not defined.
...
...


Those files are definitely located in the same proto directory.

UPDATE 2

Well, this definitely doesn't work right out of the box. Moving the files around, I was able to generate the target folder with a bunch of Java files. However, trying to compile them, I get errors for files that don't exist:

  symbol:   class CodeableConcept
  location: package com.google.fhir.stu3.proto
./PlanDefinition.java:30950: error: cannot find symbol
  public com.google.fhir.stu3.proto.CodeableConceptOrBuilder getJurisdictionOrBuilder(
                               ^
  symbol:   class CodeableConceptOrBuilder
  location: package com.google.fhir.stu3.proto
./PlanDefinition.java:30956: error: cannot find symbol
  private java.util.List<com.google.fhir.stu3.proto.CodeableConcept> topic_;
                                               ^
  symbol:   class CodeableConcept
  location: package com.google.fhir.stu3.proto
./PlanDefinition.java:30964: error: cannot find symbol
  public java.util.List<com.google.fhir.stu3.proto.CodeableConcept> getTopicList() {
                                              ^
  symbol:   class CodeableConcept
  location: package com.google.fhir.stu3.proto
./PlanDefinition.java:30974: error: cannot find symbol
  public java.util.List<? extends com.google.fhir.stu3.proto.CodeableConceptOrBuilder> 

  ....                                                         ^
  100 errors.

! Also, I don't see these file here: https://github.com/google/protobuf/tree/master/java/core/src/main/java/com/google/protobuf

回答1:

it looks like your command for compilation is not well formatted: --proto_path= This is the path to your proto --java_out= This is the folder where your class will be compiled

Your arguement is: proto/stu3/resources.proto, so I deduce that your files are located in proto/stu3.

Could you try this command and let us know if it worked for you:

protoc --proto_path=proto/stu3/ --java_out=target proto/stu3/resources.proto

If your proto descriptor are complete, you should have no compilation error, and your result should be in the folder "target".

EDIT: looking at your latest edit, I would say that you should move resources.proto into your base directory and run the following command: protoc --proto_path=. --java_out=target resources.proto

As the path to your import is coded in your import. This is a bit difficult to help, as we don't have access to your entire proto descriptor. If you took it from an online repo, it would help if you could share it.