code

Maven 프로젝트에 여러 부모가있을 수 있습니까?

codestyles 2020. 12. 31. 08:19
반응형

Maven 프로젝트에 여러 부모가있을 수 있습니까?


Java 및 Flex 프로젝트가 있습니다. 현재 두 프로젝트에 사용하려는 구성이 포함 된 기본 pom이 1 개 있습니다. 이것의 문제점은 다음과 같습니다. Flex 프로젝트는 예를 들어 for javadocpmd플러그인 같은 구성을 상속하므로 바람직하지 않습니다.

나는 다음을 정리할 진짜 기본 치어를 가지고 있고, java-base-pom과를 flex-base-pom. 그러나 Flex 부분과 Java 부분이 모두있는 다중 모듈에서 어떻게 작동합니까?

다음 구조를 사용하는 자체 애플리케이션에 대한 플러그인이 있습니다.

  • 내 플러그인
    • my-plugin-client (플렉스)
    • my-plugin-server (자바)

my-pluginpom.xmlwith <modules/>섹션 만 포함되어 있습니다 . my-pluginpom.xml을 둘 다 부모로 사용 하지만 Java base-pom 또는 flex base-pom을 부모로 사용할 수도 없습니다. 이에 대한 최선의 접근 방법은 무엇입니까?


프로젝트는 하나의 상위 (C ++의 다중 상속과 달리) 만 가질 수 있지만이 상위는 더 큰 상위 계층의 일부가 될 수 있습니다. 다른 사람들이 지적했듯이 다음과 같은 것을 가질 수 있습니다.

base-pom /
|-flex-base-pom
| |-내 플러그인 클라이언트
| | `-pom.xml
| `-pom.xml
|-자바-베이스 -pom
| |-내 플러그인 서버
| | `-pom.xml
| `-pom.xml
 `-pom.xml

즉, 실제 문제는 다음과 같다고 썼습니다.

flex 프로젝트는 예를 들어 원하지 않는 javadoc 및 pmd에 대한 구성을 상속합니다.

pluginManagement이러한 상황을 방지 하려면 요소를 사용해야합니다 .

pluginManagement 는 측면 플러그인과 함께 표시되는 요소입니다. 플러그인 관리에는이 특정 프로젝트 빌드에 대한 플러그인 정보를 구성하는 것이 아니라이 빌드에서 상속하는 프로젝트 빌드를 구성한다는 점을 제외하고는 거의 동일한 방식으로 플러그인 요소가 포함됩니다. 그러나 이것은 자식의 plugins 요소 내에서 실제로 참조되는 플러그인 만 구성합니다. 아이들은 pluginManagement 정의를 재정의 할 모든 권리가 있습니다.

따라서 부모 pom에서 플러그인을 구성하고 pluginManagement(예 : javadoc 및 pmd) plugins원하는 자식 요소 내에서 참조합니다 (여기에서는 my-plugin-server에서만). 이것은 현재 문제를 해결할 것입니다.


Maven 프로젝트에는 단일 부모가 있지만 다음과 같이 다른 pom을 가져올 수 있습니다.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>my-shared-dependencies</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

이것은 부모와 비교하여 두 가지 중요한 차이점이 있습니다.

  1. 가져온 pom에 정의 된 플러그인은 가져 오지 않습니다.
  2. 가져온 pom에 정의 된 종속성은 현재 pom에 추가되지 않고 종속성 관리 섹션으로 만 종속성을 가져옵니다.

그러나 부모 pom에 <dependency> 섹션이 있고이를 종속성에 포함하려면 일반 종속성과 마찬가지로 종속성 섹션에 부모를 추가 할 수 있습니다.

<dependency>
    <groupId>org.example</groupId>
    <artifactId>my-shared-dependencies</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

동일한 종속성을 이미 가져온 경우에도 버전 태그를 다시 지정해야합니다. 중복을 줄이기 위해 속성에 저장할 수 있습니다.


유일한 방법은 base-pom을 java-base-pom 및 flex-base-pom의 부모로 사용하는 것입니다.

내 봄 프로젝트와 비슷한 구조가 있습니다.

base-pom (basic configuration - eclipse, reports, repositories, etc)
|
+ spring-base-pom (spring definitions)
  |
  + spring-jar-base-pom (jar specific definitions)
  |
  + spring-war-base-pom (spring web and servlet dependencies)
    |
    + spring-webapp-base_pom (spring web mvc dependencies)

I've cross this exact proble also, and the best solution I found was to use Inheritance and Aggregation as suggest in this question : does maven support multiple parents (multiple inheritance) ?

You can have an aggregator pom that is not the parent of the projects it aggregates.

and explain in the Maven Documentation

Inheritance and aggregation create a nice dynamic to control builds through a single, high-level POM (...) Conversely, a POM project may aggregate projects that do not inherit from it.

From this I had my POMs inheritance (pom-master contains communes configurations, and each children the specifics ones) :

pom-master
  |-- pom-java
  |-- pom-flex

and so my project can get the specifics for each modules configurations as wished :

project (aggregate project-flex & project-java)
  |-- project-java
  |      `-- pom.xml => parent = pom-java
  |-- project-flex
  |       `-- pom.xml ==> parent = pom-flex
  `-- pom.xml => parent = pom-master

Hope it will help others as well :)


Just image that pom.xml are in fact Java classes: you can have only one parent (or extends a class), but this parent can also have another parent, and so on.

As I explained here, you must distinguish the parent and aggregation principles in Maven, which means that my-plugin would be considered as an aggregation project, not necessarily a parent project for both my-plugin-client and my-plugin-parent.

So to summarize:

my-plugin will define the base pom for all your projects. Then, you create two new pom projects: java-base-pom and flex-base-pom. They have both my-plugin as parent. Now, my-plugin-client will have java-base-pom as parent, while my-plugin-server will use flex-base-pom for his parent.

This way, my-plugin-client will inherit all properties defined in the my-plugin pom.xml, and also from java-base-pom project.


You can achieve multiple inheritance with profiles:

You create (multiple) profiles in the root pom, and auto activate any variation of these profiles achieves multiple inheritance of maven configuration.

ReferenceURL : https://stackoverflow.com/questions/1636801/can-maven-projects-have-multiple-parents

반응형