Changing order of locations on classpath to be loaded up by surefire-plugin
Asked Answered
S

2

10

Does anybody know how to change it ?

I mean from

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

to

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

It relates to this surefire-plugin feature request

It's because surefire-plugin cannot include/exclude resources from /target/classes ... it can only include/exlude resources via <testResources> element which can affect only /target/test-classes, not /target/classes

It all happens here in 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 );
}
Smallpox answered 4/1, 2011 at 11:15 Comment(7)
This smells like you are trying to solve the wrong problemHibachi
@Sean Patrick Floyed I'm sure I don't, if you read the JIRA issue, especially last 3 of my comments, I have good reason for itSmallpox
@Smallpox your comments seem to make sense, but the smell remains: There has got to be a better way to achieve what you are trying to doHibachi
@Sean Patrick Floyed you see but that is the workaround that implies from absence of the possibility to change resources in /target/classes when testing ... I really don't see why maven guys don't want it there. There are tons of such feature requests. The plugin has a Liferay's code generator, that uses META-INF/ as an output path, but I can't use META-INF when testing because I'm loading resources from dependency:[META-INF/]Smallpox
And it's a common maven dependency I cannot classload it myself ... I just have to accept that it has resources I need on this conflicting path ... All that I can do, is copy the resources over to src/test/resources, but it is a Testing Archetype, I can't do thatSmallpox
Perhaps you can tell us what you're trying to achieve. Changing the classpath order is a means how to get to it which may be ok, but perhaps the 'real' question is much easier to answer?Swetlana
We've discussed about that in the feature request. It's quite complicatedSmallpox
W
1

Consider testing in a separate project. In general, when you have a project that conflicts with The Maven Way, that's the solution - split it up.

Whitworth answered 29/6, 2011 at 16:9 Comment(0)
E
0

What I understood from your feature request link, is that you have some src/main/resources/config.xml and a dependency that also contains a config.xml that you want to use in your tests. Is that right?

If that is the case, what you could do is to move your src/main/resources/config.xml to another place (not a resource dir), like src/config/config.xml and them include it in your final JAR/WAR by setting the war or jar plugin config.

In that way your tests will see the config.xml from your dependency but not your src/config/config.xml since it is not in the classpath.

Elizabetelizabeth answered 6/12, 2011 at 3:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.