【问题标题】:Eclipse & maven output directoriesEclipse & Maven 输出目录
【发布时间】:2012-10-09 07:05:25
【问题描述】:

我正在使用 eclipse juno 和 maven 2.2.1。

有没有一种简单的方法可以将 eclipse 输出文件夹与 maven 分开? 所以我想在目标目录中构建eclipse,在target-maven目录中构建maven。

我尝试过使用

 <directory>target-maven</directory>

在 pom.xml 中。

如果我在创建项目后这样做,它工作正常。
但是当从 svn(没有 .classpath .target ... 只有 src 文件夹)和 eclipse:eclipse 恢复项目时,一切都在 target-maven 中构建。

【问题讨论】:

    标签: eclipse maven-2


    【解决方案1】:

    您可以为此创建different profiles。为 Eclipse 创建配置文件 mvn-eclipse 并为指定不同目标目录的命令行创建配置文件 mvn-cmd。您需要在 Eclipse Launch Configurations 中激活配置文件(选择 Run as -&gt; Maven Build ... 并有 Profiles 字段)或者您可以 create two different settings.xml。在一个你指定

      <activeProfiles>
        <activeProfile>mvn-eclipse</activeProfile>
      </activeProfiles>
    

    在另一个

      <activeProfiles>
        <activeProfile>mvn-cmd</activeProfile>
      </activeProfiles>
    

    您应该将包含mvn-cmd 的设置文件命名为活动配置文件settings.xml,以便在命令行上使用maven 时无需进行任何更改。在 Eclipse 中,您可以通过 Preferences -&gt; Maven -&gt; User settings 指定设置。

    但我不建议这样做,因为您可能会遇到两个输出文件夹不同步的问题。因此,请务必为此提供充分的理由。

    【讨论】:

    • 好的,谢谢。但我不知道该怎么做。我的充分理由是该项目有很多测试用例,我不想在每次构建项目时都执行它们。所以我使用-Dmaven.test.skip=true。但是,如果我进行清理,测试类会被清理但不会被重建。所以在eclipse中,如果我想运行一个简单的测试,我必须编辑类,保存,然后运行。
    • 我不认为你的理由是好的。我们曾经走过一条类似的道路,导致一个项目在一段时间后完全失灵,因为每个人都将其测试排除在运行之外。如果您的测试花费了太多时间,您可能会遇到另一个问题。当只是在 eclipse 中正常构建时,这些测试不应该被执行,你不应该对它们有问题。
    【解决方案2】:

    为了回应第一个答案,这是为 Maven 和 Eclipse 设置单独的输出文件夹的正当理由:

    我正在尝试处理一个相当大的基于 Maven 的项目(200 多个子项目),涉及最新的 Scala (2.13),Eclipse 不支持该项目。 Scala-IDE is effectively dead at 2.12。没有它,在 Eclipse 中进行干净的构建将删除 Maven 编译的 Scala 类并彻底破坏一切。我不确定是否需要提及 m2e 也无法使用。

    所以这一切都以几行 perl 来修复我的工作区结束。相信我,我已经尝试了很多其他方法。这个解决方案仍然需要我在另一个脚本步骤中将已编译的 Scala 类提供给 Eclipse,但此时这并不是真正的问题。

    此解决方案不会更改 Maven,而是 Eclipse 的输出文件夹,方法是调整 mvn eclipse:eclipse 生成的 .classpath 文件。根据自己的喜好将调用中的目标文件夹更改为setAttribute(...),然后在项目根目录中运行脚本。

    #!/usr/bin/perl
    use strict;
    use XML::XPath;
    
    my $files = [];
    find_files( '.', '^\.classpath$', $files );
    
    for my $file ( @$files ) {
        my $xp = XML::XPath->new( filename => $file );
    
        $xp->find( '/classpath/classpathentry[@kind="output"]' )
           ->[0]
           ->setAttribute( 'path', 'target-eclipse' );
    
        open my $ofh, '>', $file or die 'Cannot open for writing: '.$file;
        print $ofh $xp->getNodeAsXML;
        close $ofh;
    }
    
    sub find_files {
        my ( $path, $mask, $hits ) = @_;
        opendir my $dh, $path or die 'Cannot open path: '.$path;
        for my $entry ( grep { ! /^\.{1,2}$/ } readdir $dh ) {
            my $fullpath = $path.'/'.$entry;
            if ( -d $fullpath ) {
                find_files( $fullpath, $mask, $hits );
            } elsif ( $entry=~/$mask/ ) {
                push @$hits, $fullpath;
            }
        }
        closedir $dh;
    }
    

    更新:如果您不介意重新生成 所有 Eclipse 文件(这将清除您可能拥有的任何其他自定义设置),您可以简单地执行以下操作:

    mvn eclipse:eclipse -DoutputDirectory=target-eclipse
    

    【讨论】:

      猜你喜欢
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      相关资源
      最近更新 更多