Eclipse classpath entries only used for tests

后端 未结 5 734
我寻月下人不归
我寻月下人不归 2020-12-01 23:32

In Maven, you can have compile-time dependencies and test dependencies. This is a feature I love, and the M2Eclipse plugin makes this available in Eclipse,

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 00:16

    You could separate all your tests into another project and add the main project as a dependency (Project->Properties->Java Build Path->Projects->Add...)

    Update: To avoid changing the original project structure, your test projects can use linked locations.

    Create the test project as normal, you now need to create a linked resource to bring in the src/test/java folder. It is best to create it using a variable so that your projects can retain some platform independence. To create a new linked folder select New->Folder, input src in the folder name: field then click Advanced>>

    Click Link to folder in the file system Click on Variables... to bring up the Select Path Variable dialogue.

    If this is your first time, or you are linking to a new location select New... and give the variable a sensible name and path. If all your projects are located in c:\workspaces\foo** it makes sense to call the variable **WORKSPACE_ROOT and give it that path. If you have some other convention that is fine, but it makes sense to put a comment in the .project file so someone has a chance of figuring out what the correct value should be.

    Assuming the values above you can now set a value of WORKSPACE_ROOT/[subject project name]/src on the input field

    Once you confirm that you should see the src folder with a little arrow, and if you look in the .project file see something like this:

    
        
            src
            2
            WORKSPACE_ROOT/esf-ns-core-rp/src
        
    
    

    You can now add the src/test/java folder as a source location as normal.

    Note you can also share just the src/test/java folder by changing the config to something like this:

    
        
            src/test/java
            2
            WORKSPACE_ROOT/my-project/src/test/java
        
    
    

    This gives more control over the config, but you would have to repeat for src/test/resources, src/it/java etc.

    You then set all the test dependencies only in the test project.

    Very not pretty, but it does work (I've also used this where my test compliance level is different to the main compliance level, e.g. 1.5 for tests, but 1.4 for the target environment).

提交回复
热议问题