【问题标题】:Changing order of locations on classpath to be loaded up by surefire-plugin更改要由surefire-plugin加载的类路径上的位置顺序
【发布时间】:2011-01-04 11:15:29
【问题描述】:

有人知道怎么改吗?

我的意思是从

target/test-classes ... target/classes .... maven dependencies

target/test-classes ... maven dependencies .... target/classes 

它与这个surefire插件有关feature request

这是因为 surefire-plugin 不能包含/排除 /target/classes 中的资源 ...它只能通过 <testResources> 元素包含/排除资源,这只能影响 /target/test-classes,而不是 /target/classes

这一切都发生在 Surefire-plugin 中:

File projectClassesDirectory = new File( project.getBuild().getOutputDirectory() );
if ( !projectClassesDirectory.equals( classesDirectory ) )
{
    int indexToReplace = classpathElements.indexOf( project.getBuild().getOutputDirectory() );
    if ( indexToReplace != -1 )
    {
        classpathElements.remove( indexToReplace );
        classpathElements.add( indexToReplace, classesDirectory.getAbsolutePath() );
    }
    else
    {
        classpathElements.add( 1, classesDirectory.getAbsolutePath() );
    }
}

File projectTestClassesDirectory = new File( project.getBuild().getTestOutputDirectory() );
if ( !projectTestClassesDirectory.equals( testClassesDirectory ) )
{
    int indexToReplace = classpathElements.indexOf( project.getBuild().getTestOutputDirectory() );
    if ( indexToReplace != -1 )
    {
        classpathElements.remove( indexToReplace );
        classpathElements.add( indexToReplace, testClassesDirectory.getAbsolutePath() );
    }
    else
    {
        classpathElements.add( 0, testClassesDirectory.getAbsolutePath() );
    }
}

getLog().debug( "Test Classpath :" );

for ( Iterator i = classpathElements.iterator(); i.hasNext(); )
{
    String classpathElement = (String) i.next();

    getLog().debug( "  " + classpathElement );

    surefireBooter.addClassPathUrl( classpathElement );
}

【问题讨论】:

  • 这听起来像是你在尝试解决错误的问题
  • @Sean Patrick Floyed 我确定我没有,如果您阅读 JIRA 问题,尤其是我的最后 3 个 cmets,我有充分的理由这样做
  • @lisak 你的 cmets 似乎有道理,但气味仍然存在:必须有更好的方法来实现你想要做的事情
  • @Sean Patrick Floyed 您会看到,但这是一种解决方法,这意味着在测试时无法更改 /target/classes 中的资源......我真的不明白为什么 maven 家伙不这样做'不想要它。有很多这样的功能请求。该插件有一个 Liferay 的代码生成器,它使用 META-INF/ 作为输出路径,但是我在测试时不能使用 META-INF,因为我正在从依赖项加载资源:[META-INF/]
  • 这是一个常见的 maven 依赖项,我自己无法对其进行类加载......我只需要接受它在这条冲突的路径上有我需要的资源......我所能做的就是复制资源转到 src/test/resources,但它是一个测试原型,我不能这样做

标签: java maven-3 surefire


【解决方案1】:

考虑在单独的项目中进行测试。一般来说,当您有一个与 The Maven Way 冲突的项目时,这就是解决方案 - 拆分它。

【讨论】:

    【解决方案2】:

    我从您的功能请求链接中了解到,您有一些 src/main/resources/config.xml 和一个还包含您想在测试中使用的 config.xml 的依赖项。对吗?

    如果是这种情况,您可以做的是将您的 src/main/resources/config.xml 移动到另一个地方(不是资源目录),例如 src/config/config.xml,然后他们通过设置 war 将其包含在您的最终 JAR/WAR 中或jar 插件配置。

    这样,您的测试将看到您的依赖项中的config.xml,但看不到您的src/config/config.xml,因为它不在类路径中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2015-09-22
      相关资源
      最近更新 更多