本文介绍了如何配置Maven(命令行)和IDE以在不同的文件夹中构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在我的IDE(仅对我正在处理的代码运行单元测试)和命令行(运行所有单元测试)之间进行切换.

这会导致问题:有时,mvn clean会失败,因为IDE正在构建代码.否则我会在IDE中收到有关缺少类的奇怪错误,因为IDE正在读取Maven修改过的文件.

此外,我无法继续在IDE中使用Maven在后台进行构建的原因.

如何配置IDE或Maven以使用其他构建文件夹?

解决方案

在(父)POM中使用配置文件和此代码:

<project>
  ...

  <build>
    <outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
    <testOutputDirectory>${basedir}/${target.dir}/test-classes</testOutputDirectory>
  </build>

  <properties>
    <target.dir>target</target.dir>
  </properties>

  <profiles>
    <profile>
      <id>ide-folders</id>
      <properties>
        <target.dir>target-ide</target.dir>
      </properties>
    </profile>
  </profiles>
  ...

在IDE中配置项目,以在构建时启用配置文件target-ide.现在,Maven和IDE使用不同的文件夹.

或者,您可以在settings.xml中启用此配置文件,并将文件夹target-ide设置为默认文件夹.这样,您就不必在IDE中修改许多项目的设置.它甚至可以在您的CI构建服务器上运行(它将构建到target-ide中,但是谁在乎呢?如果您足够关心,那么也可以在其中启用配置文件).

注意:这是基于jroller.com上的一个博客帖子,该博客帖子已关闭.互联网档案馆有一个副本: https ://web.archive.org/web/20150527111413/http://www.jroller.com/eu/entry/configuring_separate_maven_output_folders

I'm often switching between my IDE (running just the unit tests for the code which I'm working on) and the command line (running all unit tests).

This causes problems: Sometimes, mvn clean fails because the IDE is building the code. Or I get weird errors in the IDE about missing classes because the IDE is reading files which Maven has modified.

Also, I can't continue working in the IDE why Maven builds in the background.

How can I configure my IDE or Maven to use different build folders?

解决方案

Use profiles and this code in your (parent) POM:

<project>
  ...

  <build>
    <outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
    <testOutputDirectory>${basedir}/${target.dir}/test-classes</testOutputDirectory>
  </build>

  <properties>
    <target.dir>target</target.dir>
  </properties>

  <profiles>
    <profile>
      <id>ide-folders</id>
      <properties>
        <target.dir>target-ide</target.dir>
      </properties>
    </profile>
  </profiles>
  ...

Configure the project in your IDE to enable the profile target-ide when building. Now Maven and the IDE use different folders.

Alternatively, you could enable this profile in your settings.xml and make the folder target-ide the default. This way, you wouldn't have to modify the settings of many projects in your IDE. It would even work on your CI build server (it would build into target-ide but who cares? And if you cared enough, you can enable the profile there as well).

Note: This is based on a blog-post on jroller.com which is down. The Internet Archive has a copy: https://web.archive.org/web/20150527111413/http://www.jroller.com/eu/entry/configuring_separate_maven_output_folders

这篇关于如何配置Maven(命令行)和IDE以在不同的文件夹中构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 20:00