每次我创建一个Maven项目时,都会有一个.iml
文件以及一个pom.xml
文件。他们的关系究竟是什么?
maven使用pom.xml来解决项目的依赖关系,插件执行以及许多其他事情。来自maven website:
项目对象模型或POM是Maven的基本工作单元。它是一个XML文件,其中包含有关Maven用于构建项目的项目和配置详细信息的信息。
另一方面,.iml
文件是IntelliJ自己的project structure的一部分。简短版本是它声明只对模块可见的库(例如jar),而不是项目的其他部分或其他项目。它是一个xml文件,包含pom.xml
中声明的每个工件的库条目及其范围(例如TEST
或COMPILE
)。例如:
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="COMPILE" name="Maven: com.google.guava:guava:18.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-library:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
</component>
</module>
我假设IntelliJ保持自己的文件格式,因此它可以更快地读取项目,无论项目使用哪个构建系统(例如maven与gradle)。
IntelliJ的想法并不了解maven project model(POM.xml)本身。在想法中创建或导入maven项目时。它还创建自己的项目结构,maven依赖项,模块详细信息等等。基本上它需要能够理解的格式的项目元数据,并在内部使用它来执行操作。这些元数据存储在.iml
文件和.idea
项目目录中。
保持自身结构的主要优势是提供更快运行和有效管理项目的能力。我希望这对这两个文件之间的关系给出一些解释。
示例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>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>2.5.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock</artifactId>
<version>${jmock.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit4</artifactId>
<version>${jmock.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junitperf</groupId>
<artifactId>junitperf</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<properties>
<jmock.version>2.5.1</jmock.version>
<junit.version>4.6</junit.version>
<log4j.version>1.2.14</log4j.version>
<org.slf4j.version>1.5.2</org.slf4j.version>
</properties>
</project>
相关的IDEA .iml文件
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.14" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-log4j12:1.5.2" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.5.2" level="project" />
<orderEntry type="library" name="Maven: org.easymock:easymock:2.5.2" level="project" />
<orderEntry type="library" name="Maven: org.jmock:jmock:2.5.1" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-library:1.1" level="project" />
<orderEntry type="library" name="Maven: org.jmock:jmock-junit4:2.5.1" level="project" />
<orderEntry type="library" name="Maven: junit:junit-dep:4.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.6" level="project" />
<orderEntry type="library" name="Maven: junitperf:junitperf:1.8" level="project" />
</component>
</module>