Where should I put test support code for a Java library

前端 未结 2 977

I have a Maven project which is used as a library in other projects. The setup is pretty standard: src/main with the library code, src/test with te

2条回答
  •  一向
    一向 (楼主)
    2021-01-18 11:51

    Here is a POM fragment which splits the main JAR into the normal artifact and a "JUnit support JAR":

    
        
            
                maven-jar-plugin
                
                    
                        default-jar
                        package
                        
                            jar
                        
                        
                            
                                true
                                
                                    true
                                
                            
                            
                                **/junit/**
                            
                        
                    
    
                    
                        junit-jar
                        package
                        
                            jar
                        
                        
                            junit
                            
                                true
                                
                                    true
                                
                            
                            
                                **/junit/**
                            
                        
                    
    
                    
                        test-jar
                        
                            test-jar
                        
                    
                
            
    
        
    
    

    In a nutshell, it looks for any package with junit in its name and puts it into the JUnit JAR, excluding it from the normal artifact.

    If the coordinate for the normal jar is

    com.group
    foo
    

    then you can get the JUnit support code by simply adding junit:

    com.group
    foo
    junit
    

    So to use this, the POM which depends on com.group:foo will look like this:

    
        com.group
        foo
        ...
    
    
        com.group
        foo
        ...
        junit
        test
    
    

    Sometimes, you will need JUnit to compile your "JUnit support JAR". If that's the case, use

    
        junit
        junit
        compile
        true
    
    

    to include JUnit into the build. This will make the dependency available at compile time but it will not leak to anyone else.

提交回复
热议问题