当我遇到一个有趣的错误时,我最近开始将一个大型Ant项目转换为一个Maven多模块项目。我一直遇到的问题是,我无法从另一个工件访问我的嵌套类,即使它们位于同一个包中(例如com.mypackage)。
对于这个项目,我们将我们的SQL查询存储在Groovy类中(多行字符串非常棒)并在我们编译的Java代码中引用它们。像这样:
ShipmentPackingQueries.groovy - 所有groovy文件都在src / main / groovy下
package com.mypackage; class ShipmentPackingQueries { static class Queries { public static class Cartons { @Language("MySQL") static final String query1 = ''' query ''' } static class CartonItems { @Language("MySQL") static final String query2 = ''' query ''' } // and so on.... } }
此文件被编译到稍后使用的jar中:mylibrary-core.jar。这些是我的maven-compiler-plugin设置 - JDK版本在标签中自动检测到,并且在我的机器上是openjdk 1.8:
<!-- defined in the parent pom that all modules in the project inherit from --> <build> <pluginManagement> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <!-- 2.8.0-01 and later require maven-compiler-plugin 3.1 or higher --> <version>3.5.1</version> <configuration> <compilerId>groovy-eclipse-compiler</compilerId> <showDeprecation>true</showDeprecation> <showWarnings>true</showWarnings> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-compiler</artifactId> <version>2.9.1-01</version> </dependency> <!-- for 2.8.0-01 and later you must have an explicit dependency on groovy-eclipse-batch --> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-eclipse-batch</artifactId> <version>2.3.7-01</version> </dependency> </dependencies> </plugin> </plugins> </pluginManagement> </build> <!-- Set explicitly in the pom files for mylibrary-core and rest-core --> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> </plugin> </plugins> </build>
当我观察工件mylibrary-core的编译时输出时,我可以看到正在成功创建类。
[writing com/mypackage/ShipmentPackingQueries$Queries$Shipments.class - #856] [writing com/mypackage/ShipmentPackingQueries$Queries$CartonTypes.class - #857] [writing com/mypackage/ShipmentPackingQueries$Queries$CartonItems.class - #858] [writing com/mypackage/ShipmentPackingQueries$Queries$Cartons.class - #859] [writing com/mypackage/ShipmentPackingQueries$Queries.class - #860] [writing com/mypackage/ShipmentPackingQueries$Queries$Feed.class - #861]
我确认这些类文件也会进入工件jar文件。问题是当我尝试使用来自另一个工件的查询时:rest-core。我已经设置了rest-core来依赖于工件mylibrary-core。这是rest-core的示例文件:
ShipmentPackingResource.java - 所有java文件都在src / main / java下
package com.mypackage; import static com.mypackage.ShipmentPackingQueries.Queries; public class ShipmentPackingResource { public void get() { Database.executeQuery(Queries.Cartons.getQuery1()); } }
工件rest-core无法编译并显示消息“Cartons无法解析或不是字段”。我只看到在查看输出时出现两次ShipmentPackingQueries:
[reading com/mypackage/ShipmentPackingQueries.class] [reading com/mypackage/ShipmentPackingQueries$Queries.class]
关于为什么maven-compiler-plugin没有读取与ShipmentPackingQueries一起使用的其他类文件的任何想法?我知道如果我将类从rest-core移动到mylibrary-core,我可以使它工作,但我想尽可能保持我的模块中的代码。谢谢!