Java 如何在多包项目中使用maven从gRPC proto生成存根

Java 如何在多包项目中使用maven从gRPC proto生成存根,java,xml,maven,grpc,rpc,Java,Xml,Maven,Grpc,Rpc,所以我已经做了2天了,我在网上搜索了几个小时,但我找不到解决这个问题的方法 这是我的代码结构: . ├── client │ ├── pom.xml -> **client pom.xml** │ └── src │ ├── main │ │ └── java │ │ └── pt │ │ └── ulisboa │ │ └── tecnico │

所以我已经做了2天了,我在网上搜索了几个小时,但我找不到解决这个问题的方法

这是我的代码结构:

.
├── client
│   ├── pom.xml -> **client pom.xml**
│   └── src
│       ├── main
│       │   └── java
│       │       └── pt
│       │           └── ulisboa
│       │               └── tecnico
│       │                   └── sec
│       │                       └── server
│       │                           ├── App.java
│                                   └── HelloClient.java
├── library
│   ├── pom.xml -> **library pom.xml**
│   └── src
│       └── main
│           └── proto
│               └── HelloWorld.proto
├── pom.xml -> **parent pom.xml**
├── server
│   ├── pom.xml -> **server pom.xml**
│   ├── server.iml
│   └── src
│       ├── main
│       │   └── java
│       │       └── pt
│       │           └── ulisboa
│       │               └── tecnico
│       │                   └── sec
│       │                       └── server
│       │                           ├── App.java
│       │                           └── HelloServer.java

目前,我有生成存根的库模块,但它们保留在pt.ulisboa.tecnico.sec.library.target.generated-sources中,但我无法在服务器或客户端访问它们

父级pom.xml如下所示:

<groupId>pt.ulisboa.tecnico.sec</groupId>
<artifactId>hdas</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<name>${project.artifactId}</name>

<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>
  <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
</properties>

  <dependencies>
        <!-- JUnit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
        <plugins>
            <!-- To run the comand mvn exec:java -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <!--<executable>java</executable>
                    <arguments>
                        <argument></argument>
                        <argument>myproject:dist</argument>
                        ...
                    </arguments>-->
                    <!--<mainClass>App</mainClass>-->

                </configuration>
            </plugin>
      ...
      </plugins>
  </build>

    <modules>
        <module>library</module>
        <module>server</module>
        <module>client</module>
    </modules>
</project>
<parent>
        <artifactId>hdas</artifactId>
        <groupId>pt.ulisboa.tecnico.sec</groupId>
        <version>1.0-SNAPSHOT</version>
</parent>


<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>



<dependencies>
  <dependency>
    <groupId>io.grpc</groupId>
      <artifactId>grpc-all</artifactId>
      <version>1.27.0</version>
   </dependency>
</dependencies>

<build>
      <extensions>
         <extension>
              <groupId>kr.motd.maven</groupId>
              <artifactId>os-maven-plugin</artifactId>
              <version>${os-maven-plugin.version}</version>
          </extension>
      </extensions>
   <pluginManagement>
        <plugin>
          <groupId>org.xolstice.maven.plugins</groupId>
          <artifactId>protobuf-maven-plugin</artifactId>
           <version>${protobuf-maven-plugin.version}</version>
          </plugin>
        </plugins>
      </pluginManagement>
<plugins>
      ...
  <plugin>
    <groupId>org.xolstice.maven.plugins</groupId>
    <artifactId>protobuf-maven-plugin</artifactId>
    <configuration>                  
    <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier} 
    </protocArtifact>
    <pluginId>grpc-java</pluginId>
    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
    <clearOutputDirectory>false</clearOutputDirectory>
    </configuration>
    <executions>
      <execution>
       <goals>
          <goal>compile</goal>
          <goal>compile-custom</goal>
        </goals>
       </execution>
     </executions>
   </plugin>
 </plugins>
</build>

<parent>
  <artifactId>hdas</artifactId>
  <groupId>pt.ulisboa.tecnico.sec</groupId>
  <version>1.0-SNAPSHOT</version>
</parent>


<!--<groupId>pt.ulisboa.tecnico.sec</groupId>-->

    <artifactId>server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

  <name>${project.artifactId}</name>


  <properties>
      <!-- compiler properties -->
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>

      <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>

      <!-- application properties -->
      <mainclass>pt.ulisboa.tecnico.sec.server.HelloServer</mainclass>

      <server.port>8080</server.port>
  </properties>

  <dependencies>
    <!-- contract brings gRPC dependencies with it -->
    <dependency>
       <groupId>pt.ulisboa.tecnico.sec</groupId>
        <artifactId>library</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

      <dependency>
          <groupId>io.grpc</groupId>
          <artifactId>grpc-all</artifactId>
          <version>1.27.0</version>
      </dependency>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
          <!-- Plugin that provides two goals to execute system and Java programs -->
          <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>exec-maven-plugin</artifactId>
              <version>${exec-maven-plugin.version}</version>
              <executions>
                  <execution>
                      <goals>
                          <goal>java</goal>
                      </goals>
                  </execution>
              </executions>
              <configuration>
                  <!-- to prevent deprecation warning: -->
                  <killAfter>-1</killAfter>
                  <mainClass>${mainclass}</mainClass>
                  <arguments>
                      <argument>${server.port}</argument>
                  </arguments>
              </configuration>
          </plugin>
     </plugins>
    </pluginManagement>
  </build>
</project>

pt.ulisboa.tecnico.sec
hdas
聚甲醛
1.0-快照
${project.artifactId}
UTF-8
1.7
1.7
1.6.0
朱尼特
朱尼特
4.11
测试
org.codehaus.mojo
execmaven插件
1.6.0
...
图书馆
服务器
客户
库pom.xml如下所示:

<groupId>pt.ulisboa.tecnico.sec</groupId>
<artifactId>hdas</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<name>${project.artifactId}</name>

<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>
  <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
</properties>

  <dependencies>
        <!-- JUnit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
        <plugins>
            <!-- To run the comand mvn exec:java -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <!--<executable>java</executable>
                    <arguments>
                        <argument></argument>
                        <argument>myproject:dist</argument>
                        ...
                    </arguments>-->
                    <!--<mainClass>App</mainClass>-->

                </configuration>
            </plugin>
      ...
      </plugins>
  </build>

    <modules>
        <module>library</module>
        <module>server</module>
        <module>client</module>
    </modules>
</project>
<parent>
        <artifactId>hdas</artifactId>
        <groupId>pt.ulisboa.tecnico.sec</groupId>
        <version>1.0-SNAPSHOT</version>
</parent>


<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>



<dependencies>
  <dependency>
    <groupId>io.grpc</groupId>
      <artifactId>grpc-all</artifactId>
      <version>1.27.0</version>
   </dependency>
</dependencies>

<build>
      <extensions>
         <extension>
              <groupId>kr.motd.maven</groupId>
              <artifactId>os-maven-plugin</artifactId>
              <version>${os-maven-plugin.version}</version>
          </extension>
      </extensions>
   <pluginManagement>
        <plugin>
          <groupId>org.xolstice.maven.plugins</groupId>
          <artifactId>protobuf-maven-plugin</artifactId>
           <version>${protobuf-maven-plugin.version}</version>
          </plugin>
        </plugins>
      </pluginManagement>
<plugins>
      ...
  <plugin>
    <groupId>org.xolstice.maven.plugins</groupId>
    <artifactId>protobuf-maven-plugin</artifactId>
    <configuration>                  
    <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier} 
    </protocArtifact>
    <pluginId>grpc-java</pluginId>
    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
    <clearOutputDirectory>false</clearOutputDirectory>
    </configuration>
    <executions>
      <execution>
       <goals>
          <goal>compile</goal>
          <goal>compile-custom</goal>
        </goals>
       </execution>
     </executions>
   </plugin>
 </plugins>
</build>

<parent>
  <artifactId>hdas</artifactId>
  <groupId>pt.ulisboa.tecnico.sec</groupId>
  <version>1.0-SNAPSHOT</version>
</parent>


<!--<groupId>pt.ulisboa.tecnico.sec</groupId>-->

    <artifactId>server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

  <name>${project.artifactId}</name>


  <properties>
      <!-- compiler properties -->
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>

      <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>

      <!-- application properties -->
      <mainclass>pt.ulisboa.tecnico.sec.server.HelloServer</mainclass>

      <server.port>8080</server.port>
  </properties>

  <dependencies>
    <!-- contract brings gRPC dependencies with it -->
    <dependency>
       <groupId>pt.ulisboa.tecnico.sec</groupId>
        <artifactId>library</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

      <dependency>
          <groupId>io.grpc</groupId>
          <artifactId>grpc-all</artifactId>
          <version>1.27.0</version>
      </dependency>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
          <!-- Plugin that provides two goals to execute system and Java programs -->
          <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>exec-maven-plugin</artifactId>
              <version>${exec-maven-plugin.version}</version>
              <executions>
                  <execution>
                      <goals>
                          <goal>java</goal>
                      </goals>
                  </execution>
              </executions>
              <configuration>
                  <!-- to prevent deprecation warning: -->
                  <killAfter>-1</killAfter>
                  <mainClass>${mainclass}</mainClass>
                  <arguments>
                      <argument>${server.port}</argument>
                  </arguments>
              </configuration>
          </plugin>
     </plugins>
    </pluginManagement>
  </build>
</project>


hdas
pt.ulisboa.tecnico.sec
1.0-快照
图书馆
1.0-快照
罐子
${project.artifactId}
io.grpc
grpc全部
1.27.0
马文
os maven插件
${os maven plugin.version}
org.xolstice.maven.plugins
protobuf maven插件
${protobuf maven plugin.version}
...
org.xolstice.maven.plugins
protobuf maven插件
protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
GRPCJava
io.grpc:protoc gen grpc java:${grpc.version}:exe:${os.detected.classifier}
假的
编译
编译自定义
服务器pom.xml如下所示:

<groupId>pt.ulisboa.tecnico.sec</groupId>
<artifactId>hdas</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<name>${project.artifactId}</name>

<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>
  <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
</properties>

  <dependencies>
        <!-- JUnit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
        <plugins>
            <!-- To run the comand mvn exec:java -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <!--<executable>java</executable>
                    <arguments>
                        <argument></argument>
                        <argument>myproject:dist</argument>
                        ...
                    </arguments>-->
                    <!--<mainClass>App</mainClass>-->

                </configuration>
            </plugin>
      ...
      </plugins>
  </build>

    <modules>
        <module>library</module>
        <module>server</module>
        <module>client</module>
    </modules>
</project>
<parent>
        <artifactId>hdas</artifactId>
        <groupId>pt.ulisboa.tecnico.sec</groupId>
        <version>1.0-SNAPSHOT</version>
</parent>


<artifactId>library</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>



<dependencies>
  <dependency>
    <groupId>io.grpc</groupId>
      <artifactId>grpc-all</artifactId>
      <version>1.27.0</version>
   </dependency>
</dependencies>

<build>
      <extensions>
         <extension>
              <groupId>kr.motd.maven</groupId>
              <artifactId>os-maven-plugin</artifactId>
              <version>${os-maven-plugin.version}</version>
          </extension>
      </extensions>
   <pluginManagement>
        <plugin>
          <groupId>org.xolstice.maven.plugins</groupId>
          <artifactId>protobuf-maven-plugin</artifactId>
           <version>${protobuf-maven-plugin.version}</version>
          </plugin>
        </plugins>
      </pluginManagement>
<plugins>
      ...
  <plugin>
    <groupId>org.xolstice.maven.plugins</groupId>
    <artifactId>protobuf-maven-plugin</artifactId>
    <configuration>                  
    <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier} 
    </protocArtifact>
    <pluginId>grpc-java</pluginId>
    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
    <clearOutputDirectory>false</clearOutputDirectory>
    </configuration>
    <executions>
      <execution>
       <goals>
          <goal>compile</goal>
          <goal>compile-custom</goal>
        </goals>
       </execution>
     </executions>
   </plugin>
 </plugins>
</build>

<parent>
  <artifactId>hdas</artifactId>
  <groupId>pt.ulisboa.tecnico.sec</groupId>
  <version>1.0-SNAPSHOT</version>
</parent>


<!--<groupId>pt.ulisboa.tecnico.sec</groupId>-->

    <artifactId>server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

  <name>${project.artifactId}</name>


  <properties>
      <!-- compiler properties -->
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>

      <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>

      <!-- application properties -->
      <mainclass>pt.ulisboa.tecnico.sec.server.HelloServer</mainclass>

      <server.port>8080</server.port>
  </properties>

  <dependencies>
    <!-- contract brings gRPC dependencies with it -->
    <dependency>
       <groupId>pt.ulisboa.tecnico.sec</groupId>
        <artifactId>library</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

      <dependency>
          <groupId>io.grpc</groupId>
          <artifactId>grpc-all</artifactId>
          <version>1.27.0</version>
      </dependency>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
          <!-- Plugin that provides two goals to execute system and Java programs -->
          <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>exec-maven-plugin</artifactId>
              <version>${exec-maven-plugin.version}</version>
              <executions>
                  <execution>
                      <goals>
                          <goal>java</goal>
                      </goals>
                  </execution>
              </executions>
              <configuration>
                  <!-- to prevent deprecation warning: -->
                  <killAfter>-1</killAfter>
                  <mainClass>${mainclass}</mainClass>
                  <arguments>
                      <argument>${server.port}</argument>
                  </arguments>
              </configuration>
          </plugin>
     </plugins>
    </pluginManagement>
  </build>
</project>


hdas
pt.ulisboa.tecnico.sec
1.0-快照
服务器
1.0-快照
罐子
${project.artifactId}
UTF-8
UTF-8
1.8
1.8
1.6.0
pt.ulisboa.tecnico.sec.server.HelloServer
8080
pt.ulisboa.tecnico.sec
图书馆
1.0-快照
io.grpc
grpc全部
1.27.0
朱尼特
朱尼特
4.12
测试
org.codehaus.mojo
execmaven插件
${exec maven plugin.version}
JAVA
-1
${mainclass}
${server.port}
客户机与服务器非常相似

我怎样才能让它工作

我尝试了google的简单hello world,但由于它们不是不同的软件包,所以客户端和服务器旁边都出现了一个ServiceImpl(我想这就是原因)

运行
path to proj/library$mvn clean install
运行得很好,确实生成了所需的代码,如上所述,但每次我尝试编译和运行服务器时,它都会说
找不到符号
->它无法识别先前假定生成的对象

我目前正在运行gRPC hello world,但正在尝试将其调整为多模块系统

抱歉,如果代码太多,或者不够具体,我还是新手:)

感谢所有的帮助和纠正