在apple M1芯片的Mac中,使用protobuf进行类的自动生成时,会报protoc的依赖会找不到的错误,如下所示:
[ERROR] 1) com.google.protobuf:protoc:exe:osx-aarch_64:3.1.0
[ERROR]
[ERROR] Try downloading the file manually from the project website.
[ERROR]
[ERROR] Then, install it using the command:
[ERROR] mvn install:install-file -DgroupId=com.google.protobuf -DartifactId=protoc -Dversion=3.1.0 -Dclassifier=osx-aarch_64 -Dpackaging=exe -Dfile=/path/to/file
[ERROR]
[ERROR] Alternatively, if you host your own repository you can deploy the file there:
[ERROR] mvn deploy:deploy-file -DgroupId=com.google.protobuf -DartifactId=protoc -Dversion=3.1.0 -Dclassifier=osx-aarch_64 -Dpackaging=exe -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
使用的protoc依赖如下:
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protoc</artifactId>
<version>3.1.0</version>
</dependency>
【原因】是protoc没有提供arm版的依赖,需要替换成osx-x86_64。
下面是protobuf插件的配置内容,根据配置内容来介绍替换过程。
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>
com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>
io.grpc:protoc-gen-grpc-java:1.11.0:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
替换过程需要注意几点:
1 其中${os.detected.classifier}便是识别系统芯片架构的占位符
2 直接将该占位符替换成osx-x86_64,可以解决问题,但不是最佳,这相当于是硬编码,在项目协作中可能会给其他非arm架构芯片的开发人员带来不便
3 【推荐方式】在maven的配置文件setting.xml中进行定义os.detected.classifier的定义,定义方式如下所示,新增一个profile,将os.detected.classifier定义好,然后在activeProfiles中要将该profile生效才有用,生效这一步很关键
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>
</profile>
<profile>
<id>apple-silicon</id>
<properties>
<os.detected.classifier>osx-x86_64</os.detected.classifier>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
<activeProfile>apple-silicon</activeProfile>
</activeProfiles>
截图重点强调下:
4 protoc的maven依赖新增type和classifier,如下所示
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protoc</artifactId>
<version>3.1.0</version>
<type>exe</type>
<classifier>osx-x86_64</classifier>
</dependency>