【问题标题】:Dependencies in pom.xml profilespom.xml 配置文件中的依赖项
【发布时间】:2017-08-29 09:00:27
【问题描述】:

我的问题是当我向配置文件添加依赖项时,代码中的所有内容都会“无法解析”。那个 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">
<modelVersion>4.0.0</modelVersion>

<groupId>aaa</groupId>
<artifactId>zzz</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>test</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<profiles>
    <profile>
        <id>dev</id>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>

           ... more dependencies...
        </dependencies>
    </profile>
    <profile>
        <id>test</id>
        <dependencies>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>1.4.196</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

还有一个问题 - 如果我有配置文件,某些依赖项是否可以在没有任何配置文件的情况下成为每个配置文件的一部分?感谢您的回答。

【问题讨论】:

  • 如果你想使用它,你必须激活一个配置文件。但是,当您在代码中实际需要依赖项时,为什么还要在配置文件中添加依赖项呢?
  • 我粘贴了错误的pom,现在已经编辑了。 :) 我想在测试中有其他数据库。
  • 我在 application.properties 中有 spring.profiles.active=dev

标签: java spring maven spring-boot pom.xml


【解决方案1】:

您可以在配置文件内部和外部拥有依赖项。如果您想使用配置文件,您需要激活它,无论是从命令行还是通过文件等激活触发器。

【讨论】:

  • 好的,我知道我可以在运行例如 mvn clean install -P dev 并且当我的测试运行时激活配置文件,但在 IntelliJ 中,整个代码“无法解析” "。
  • 您可能需要告诉 IntelliJ 在 Workspace 中激活哪些配置文件(在 Eclipse 中,m2e 插件有相应的选项)。
猜你喜欢
  • 2022-12-18
  • 1970-01-01
  • 2021-06-12
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
相关资源
最近更新 更多