如果我希望项目A
能够编译并运行测试,但是当我将它作为依赖项放入项目B
时,项目A
的依赖项不应该可用于项目{ {1}}。
例如:
将B
作为依赖项添加到项目org.example.foo
(不是A
)
在项目B
A
作为依赖项
在项目B
内的类中添加此语句:B
您应该在此行收到编译错误:import org.example.foo.*
答案 0 :(得分:2)
您可以通过以下排除项排除特定的传递依赖项:
<project>
...
<dependencies>
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
或者您可以排除所有传递依赖关系(需要Maven 3.2.1+):
<project>
...
<dependencies>
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
除此之外,如果您可能需要更改项目A以使用不同的依赖项等,我会考虑您的项目结构。
答案 1 :(得分:1)
对于Gradle,您可以使用gradle-dependency-analyze插件,它会添加analyzeClassesDependencies
任务:
此任务取决于
classes
任务并分析依赖关系 主要源集的输出目录。这确保了所有 类的依赖关系在compile
中声明,compileOnly
或provided
配置。它也确保了 反之,使用这些配置的所有依赖关系 按类
因此它不会触发编译问题,但如果B
尝试导入org.example.foo.*
,则在B
构建期间您将收到此消息:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':B:analyzeClassesDependencies'.
> Dependency analysis found issues: usedUndeclaredArtifacts:
- org.example.foo:foo-core:1.0.0
B
需要自己的foo-core
依赖关系(它不能使用A
&#39; s)或者不允许A
<?xml version="1.0" encoding="UTF-8"?>
<DATA_DS>
<Parent_Group>
<Employee_Traversal_Record>
<Employee_1_Child_Group>
<Employee_1_Detail_Record>
<Employee_Number>12345</Employee_Number>
<Last_Name>Doe</Last_Name>
<First_Name>John</First_Name>
<Date_of_Birth>1964-02-28T00:00:00.000Z</Date_of_Birth>
<Standard_Working_Hours>40</Standard_Working_Hours>
<Plan_Name>B Plan</Plan_Name>
<Coverage_Start_Date>2018-01-01T00:00:00.000Z</Coverage_Start_Date>
<Plan_Type>B</Plan_Type>
<Dependent_Child_Group>
<Dependent_Detail_Record>
<Parent_Employee_Number>12345</Parent_Employee_Number>
<Dependent_Last_Name>Doe</Dependent_Last_Name>
<Dependent_First_Name>Dependent1</Dependent_First_Name>
<Dependent_Birth_Date>1998-09-03T00:00:00.000Z</Dependent_Birth_Date>
<Relationship_Status>A</Relationship_Status>
<Plan_Name>B Plan</Plan_Name>
<Plan_Type>B</Plan_Type>
</Dependent_Detail_Record>
</Dependent_Child_Group>
<Dependent_Child_Group>
<Dependent_Detail_Record>
<Parent_Employee_Number>12345</Parent_Employee_Number>
<Dependent_Last_Name>Doe</Dependent_Last_Name>
<Dependent_First_Name>Dependent2</Dependent_First_Name>
<Dependent_Birth_Date>1960-11-08T00:00:00.000Z</Dependent_Birth_Date>
<Relationship_Status>A</Relationship_Status>
<Plan_Name>B Plan</Plan_Name>
<Plan_Type>B</Plan_Type>
</Dependent_Detail_Record>
</Dependent_Child_Group>
<Dependent_Child_Group>
<Dependent_Detail_Record>
<Parent_Employee_Number>12345</Parent_Employee_Number>
<Dependent_Last_Name>Doe</Dependent_Last_Name>
<Dependent_First_Name>Dependent3</Dependent_First_Name>
<Dependent_Birth_Date>1993-09-09T00:00:00.000Z</Dependent_Birth_Date>
<Relationship_Status>A</Relationship_Status>
<Plan_Name>B Plan</Plan_Name>
<Plan_Type>B</Plan_Type>
</Dependent_Detail_Record>
</Dependent_Child_Group>
</Employee_1_Detail_Record>
</Employee_1_Child_Group>
<Employee_1_Child_Group>
<Employee_1_Detail_Record>
<Employee_Number>12345</Employee_Number>
<Last_Name>Doe</Last_Name>
<First_Name>John</First_Name>
<Date_of_Birth>1964-02-28T00:00:00.000Z</Date_of_Birth>
<Standard_Working_Hours>40</Standard_Working_Hours>
<Plan_Name>A Plan</Plan_Name>
<Coverage_Start_Date>2018-01-01T00:00:00.000Z</Coverage_Start_Date>
<Plan_Type>A</Plan_Type>
<Dependent_Child_Group>
<Dependent_Detail_Record>
<Parent_Employee_Number>12345</Parent_Employee_Number>
<Dependent_Last_Name>Doe</Dependent_Last_Name>
<Dependent_First_Name>Dependent1</Dependent_First_Name>
<Dependent_Birth_Date>1998-09-03T00:00:00.000Z</Dependent_Birth_Date>
<Relationship_Status>A</Relationship_Status>
<Plan_Name>A Plan</Plan_Name>
<Plan_Type>A</Plan_Type>
</Dependent_Detail_Record>
</Dependent_Child_Group>
<Dependent_Child_Group>
<Dependent_Detail_Record>
<Parent_Employee_Number>12345</Parent_Employee_Number>
<Dependent_Last_Name>Doe</Dependent_Last_Name>
<Dependent_First_Name>Dependent2</Dependent_First_Name>
<Dependent_Birth_Date>1960-11-08T00:00:00.000Z</Dependent_Birth_Date>
<Relationship_Status>A</Relationship_Status>
<Plan_Name>A Plan</Plan_Name>
<Plan_Type>A</Plan_Type>
</Dependent_Detail_Record>
</Dependent_Child_Group>
<Dependent_Child_Group>
<Dependent_Detail_Record>
<Parent_Employee_Number>12345</Parent_Employee_Number>
<Dependent_Last_Name>Doe</Dependent_Last_Name>
<Dependent_First_Name>Dependent3</Dependent_First_Name>
<Dependent_Birth_Date>1993-09-09T00:00:00.000Z</Dependent_Birth_Date>
<Relationship_Status>A</Relationship_Status>
<Plan_Name>A Plan</Plan_Name>
<Plan_Type>A</Plan_Type>
</Dependent_Detail_Record>
</Dependent_Child_Group>
</Employee_1_Detail_Record>
</Employee_1_Child_Group>
<Employee_1_Child_Group>
<Employee_1_Detail_Record>
<Employee_Number>12345</Employee_Number>
<Last_Name>Doe</Last_Name>
<First_Name>John</First_Name>
<Date_of_Birth>1964-02-28T00:00:00.000Z</Date_of_Birth>
<Standard_Working_Hours>40</Standard_Working_Hours>
<Plan_Name>C Plan</Plan_Name>
<Coverage_Start_Date>2018-01-01T00:00:00.000Z</Coverage_Start_Date>
<Plan_Type>C</Plan_Type>
<Dependent_Child_Group>
<Dependent_Detail_Record>
<Parent_Employee_Number>12345</Parent_Employee_Number>
<Dependent_Last_Name>Doe</Dependent_Last_Name>
<Dependent_First_Name>Dependent3</Dependent_First_Name>
<Dependent_Birth_Date>1993-09-09T00:00:00.000Z</Dependent_Birth_Date>
<Relationship_Status>A</Relationship_Status>
<Plan_Name>C Plan</Plan_Name>
<Plan_Type>C</Plan_Type>
</Dependent_Detail_Record>
</Dependent_Child_Group>
<Dependent_Child_Group>
<Dependent_Detail_Record>
<Parent_Employee_Number>12345</Parent_Employee_Number>
<Dependent_Last_Name>Doe</Dependent_Last_Name>
<Dependent_First_Name>Dependent2</Dependent_First_Name>
<Dependent_Birth_Date>1960-11-08T00:00:00.000Z</Dependent_Birth_Date>
<Relationship_Status>A</Relationship_Status>
<Plan_Name>C Plan</Plan_Name>
<Plan_Type>C</Plan_Type>
</Dependent_Detail_Record>
</Dependent_Child_Group>
<Dependent_Child_Group>
<Dependent_Detail_Record>
<Parent_Employee_Number>12345</Parent_Employee_Number>
<Dependent_Last_Name>Doe</Dependent_Last_Name>
<Dependent_First_Name>Dependent1</Dependent_First_Name>
<Dependent_Birth_Date>1998-09-03T00:00:00.000Z</Dependent_Birth_Date>
<Relationship_Status>A</Relationship_Status>
<Plan_Name>C Plan</Plan_Name>
<Plan_Type>C</Plan_Type>
</Dependent_Detail_Record>
</Dependent_Child_Group>
</Employee_1_Detail_Record>
</Employee_1_Child_Group>
</Employee_Traversal_Record>
</Parent_Group>
</DATA_DS>
依赖关系仍然无法使用(无法构建)
答案 2 :(得分:1)
此gradle-dependency-analyze中提到的answer插件的自述文件提及:
此插件尝试复制maven的功能 依赖插件分析目标,如果依赖,则构建失败 声明但未使用或使用但未声明。
取得这一领先优势,当我检查了maven依赖插件的dependency:analyze目标时,我找到了failOnWarning选项,这可能就是你所追求的。
因此,您可以将其插入到项目B
的maven调用中,如下所示:
mvn -DfailOnWarning = true依赖:analyze&lt;其他目标,包&gt;
并且,如果存在任何Used undeclared dependencies
或Unused declared dependencies
的实例,前者是您的用例,则构建将失败。
唯一的缺点是,我没有找到任何命令行方式让插件忽略Unused declared dependencies
。
答案 3 :(得分:1)
您应该使用<scope>provided</scope>
指定项目“A”中的依赖项。这样,相应的依赖关系用于编译&amp;测试,但不能在项目“B”中传递使用。
在这里,您可以找到maven依赖项的不同范围:https://maven.apache.org/pom.html#Dependencies