我有一个具有以下结构的多模块Maven项目
Foo
|_ Bar
| |_ src/main/java/bar/BarClass.java
| |_ pom.xml
|
|_ Baz
| |_ src/main/java/baz
| |_ pom.xml |_ BazClass.java
| |_ SerializablePlugin.java
|_ pom.xml
模块Baz使用Byte Buddy插件为Maven构建的一部分向BazClass
添加接口。对于此示例,我刚刚选择了Serializable
。
package baz;
public class BazClass // Serializable will be added by Byte Buddy
{
}
为了使用BazClass
,模块Bar依赖于Baz。
package bar;
import baz.BazClass;
import java.io.Serializable;
public class BarClass
{
public static void main(String[] args)
{
Serializable serializable = new BazClass();
}
}
使用Maven可以很好地编译-查看反编译的类文件,我可以看到已经添加了接口-但IntelliJ报告错误,因为它不知道修改。
Incompatible types.
Required: java.io.Serializable
Found: baz.BazClass
因此,我只能推断出它必须查看源文件才能做出此决定,而不是编译后的类文件。
是否可以更改IntelliJ设置,以便它可以查看类文件而不是源文件中的定义?如果这不是一个多模块项目,而我是从存储库中提取JAR,那么我就不会遇到这个问题。
我强烈希望不要仅因为与IDE有关的问题而更改构建。
如果要尝试复制,下面是上面未显示的其余POM和类。道歉,它不短;我已尽可能减少它,但POM太冗长。
Foo
<?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>foo</groupId>
<artifactId>foo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<modules>
<module>bar</module>
<module>baz</module>
</modules>
</project>
酒吧
POM
<?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">
<parent>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bar</artifactId>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>foo</groupId>
<artifactId>baz</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
巴兹
POM
<?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">
<parent>
<artifactId>foo</artifactId>
<groupId>foo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>baz</artifactId>
<dependencies>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.9.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-maven-plugin</artifactId>
<version>1.9.12</version>
<executions>
<execution>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformations>
<transformation>
<plugin>baz.SerializablePlugin</plugin>
</transformation>
</transformations>
</configuration>
</plugin>
</plugins>
</build>
</project>
插件
package baz;
import net.bytebuddy.build.Plugin;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import java.io.Serializable;
public class SerializablePlugin implements Plugin
{
@Override
public DynamicType.Builder<?> apply(final DynamicType.Builder<?> builder,
final TypeDescription typeDescription,
final ClassFileLocator classFileLocator)
{
return builder.topLevelType().implement(Serializable.class);
}
@Override
public boolean matches(final TypeDescription target)
{
return target.getSimpleName().equals("BazClass");
}
@Override
public void close()
{
// not required
}
}