2017-08-29 81 views
0

我的问题是,当我向配置文件添加依赖关系时,代码中的所有内容都得到“无法解析”。那个pom有什么问题?pom.xml配置文件中的依赖关系

<?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> 

还有一个问题 - 如果我有谱,可一些依赖的是不带任何个人资料是每个配置文件的一部分?谢谢你的回答。

+0

如果您想使用它,您必须_activate_配置文件。但为什么在你的代码中实际需要它们时在配置文件中存在依赖关系? – Seelenvirtuose

+0

我粘贴了错误的POM,现在编辑它。 :)我想在测试中有其他数据库。 – Helosze

+0

我在application.properties中有spring.profiles.active = dev – Helosze

回答

1

您可以在配置文件内部和外部有依赖关系。如果你想使用一个配置文件,你需要激活它,无论是从命令行还是通过一些激活触发器就像一个文件。

+0

好了,我知道我可以在运行例如mvn clean install -P dev时激活配置文件,并且当我的测试运行时,配置文件正在更改,但在IntelliJ整个代码都有“无法解析”。 – Helosze

+1

您可能需要告知IntelliJ要在Workspace中激活哪些配置文件(在Eclipse中,m2e插件具有该选项)。 –