仅导入使用Maven构建的库的一个配置文件

时间:2018-07-19 11:42:48

标签: java maven build

我的pom.xml中有两个配置文件。一个默认值和另一个用于构建swagger客户端库。我将此库导入另一个Maven项目。问题在于它使用了第一个项目的所有依赖项,甚至包括默认配置文件中提到的依赖项。解决问题的最佳方法是什么?生成jar时是否可以在pom文件中仅放置一个配置文件?还是在导入依赖项时选择配置文件?

1 个答案:

答案 0 :(得分:1)

您需要这样的东西

1)父母将其全部保存在一起

<?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>com.greg</groupId>
  <artifactId>hm-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>hm-parent</name>

  <modules>
    <module>hm-client</module>
    <module>hm-default</module>
  </modules>

</project>

2)客户端模块

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.greg</groupId>
    <artifactId>hm-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>hm-client</artifactId>
  <name>hm-client</name>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

</project>

3)一个默认模块,可将客户端从同一父项下的同一项目中拉入

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>com.greg</groupId>
    <artifactId>hm-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>hm-default</artifactId>
  <name>hm-default</name>

  <dependencies>
    <dependency>
      <groupId>com.greg</groupId>
      <artifactId>hm-client</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

</project>