【问题标题】:How to reasonable rename package with maven plugin如何使用 maven 插件合理重命名包
【发布时间】:2017-07-03 07:26:09
【问题描述】:

我的项目有以下模块:

  • 客户
  • 休息

上述模块都依赖于com.google.protobufrest 依赖于clientrest 模块使用protobuf jar by client)。

为了避免冲突,我在带有shade插件的client模块中将com.google.protobuf重命名为my.com.google.protobuf

问题是rest模块无法编译,报如下错误:

error: incompatible types: my.com.google.protobuf.Descriptors.FileDescriptor cannot be converted to com.google.protobuf.Descriptors.FileDescriptor

客户端 pom.xml

    <parent>
        <groupId>my-project</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>client</artifactId>
    <name>Client</name>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>${protobuf.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <filters>
                        <filter>
                            <artifact>client</artifact>
                            <includes>
                                <include>**/*.class</include>
                            </includes>
                        </filter>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>io.my.client.Client</mainClass>
                                </transformer>
                            </transformers>
                            <relocations>
                                <relocation>
                                    <pattern>my.google</pattern>
                                    <shadedPattern>shiva.com.google</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

休息 pom.xml

     <parent>
        <groupId>my-project</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>rest</artifactId>

    <dependencies>
        <dependency>
            <groupId>my-project</groupId>
            <artifactId>clientt</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <filters>
                        <filter>
                            <artifact>rest</artifact>
                            <includes>
                                <include>**/*.class</include>
                            </includes>
                        </filter>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>io.my.rest.WebServer</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • 请出示您的代码 (minimal reproducible example)
  • 您以完全错误的方式使用com.google.protobuf 和阴影插件。 REST 模块依赖于客户端,这听起来很奇怪。
  • @JimHawkins 这个rest提供了restful API来访问客户端API

标签: maven maven-plugin


【解决方案1】:

更好的解决方法似乎是:在 IntelliJ 的项目视图中右键单击项目“client”-> pom.xml,选择“Maven”->“Ignore Projects”。然后在顶级 pom.xml 上执行“Maven”->“重新导入”。

【讨论】:

    猜你喜欢
    • 2013-02-16
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多