2016-01-04 21 views
5

Bir maven plugin veMaven Plugin Mojo yapılandır Varsayılan Parametre

public abstract class AbstractSetupMojo extends AbstractMojo { 
    @Parameter(property="targetHost", defaultValue="localhost") 
    private String targetHost; 
    @Parameter(property="targetPort", defaultValue="27017") 
    private Integer targetPort; 
    @Parameter(property="targetDbName", required=true) 
    private String targetDbName; 
    @Parameter(property="sourceHost", defaultValue="${mojo.configuration.targetHost}") 
    private String sourceHost; 
    @Parameter(property="sourcePort", defaultValue="${mojo.configuration.targetPort}") 
    private Integer sourcePort; 
    @Parameter (property="sourceDbName", defaultValue="${mojo.configuration.targetDbName}") 
    private String sourceDbName; 
    @Parameter(property="scriptsPath") 
    private File scriptsPath; 
}  

Diğer Mojos bu bir uzatıyoruz biraz yakın görünüyor basit Mojo var Değerleri. Yani, fikir source* parametrelerini ilgili target* parametrelerinin değerlerine ayarlamaktır. Ayrıca bazı varsayılan değerler ayarlanmıştır. testte

Ben

public class GenerateMojoTest extends AbstractMojoTestCase { 

    protected void setUp() throws Exception { 
     super.setUp(); 
    } 

    public void testConfiguration() throws Exception { 
     File pom = getTestFile("src/test/resources/test-project-1/pom.xml"); 
     assertNotNull(pom); 
     assertTrue(pom.exists()); 
     GenerateMojo generateMojo = (GenerateMojo)lookupMojo("generate", pom); 
     assertThat(generateMojo.getSourceDbName()).isEqualTo(generateMojo.getTargetDbName()); 
     assertThat(generateMojo.getSourcePort()).isEqualTo(generateMojo.getTargetPort()).isEqualTo(27017); 
     assertThat(generateMojo.getSourceHost()).isEqualTo(generateMojo.getTargetHost()).isEqualTo("localhost"); 
    } 

} 

gibi bir şey ben eğer tüm Mojo parametreler boş olduğu için test başarısız

 <plugin> 
      <groupId>com.ffy</groupId> 
      <artifactId>setup-maven-plugin</artifactId> 
    <!--     
      <configuration> 
       <scriptsPath>src/test/resources/temp</scriptsPath> 
      </configuration> 
    -->     
      <executions> 
       <execution> 
        <id>generate</id> 
        <goals> 
         <goal>generate</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 

gibi testinde POM dosyasında ilgi parçası görünüyor <configuration> yorumunu beklemedim, eğer bunu uncomment yaparsam, o zaman sadece scriptsPath belirlenir. Diğer parametreler null.

Tamamen yapılandırılmış bir Mojo yapmak için testlerimde başka bir şey yapmak zorunda mıyım?

Ben

protected GenerateMojo setupMojo(final File pom) throws ComponentConfigurationException, Exception { 
    final MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); 
    final ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest(); 
    final ProjectBuilder projectBuilder = this.lookup(ProjectBuilder.class); 
    final MavenProject project = projectBuilder.build(pom, buildingRequest).getProject(); 
    final MavenSession session = newMavenSession(project); 
    final MojoExecution execution = newMojoExecution("generate"); 
    final GenerateMojo mojo = (GenerateMojo) this.lookupConfiguredMojo(session, execution); 
    return mojo; 
} 

yerine lookupMojo ile uzun bir yaklaşım denedik ama bu biraz değişmedi.

cevap

5

Sen biraz daha akıllı olması beklenebilir. <configuration/> parçası olması ve ilgilendiğiniz değerleri tanımlamak gerekir, ama aslında test koşum olduğunu mu ne o <configuration/> değerleri okur ve ek açıklamalarınızdakileri yok sayar. Test demeti arzulanan bir çok şey bırakıyor, aslında sizin için uygun bir Maven uygulaması/enterpolasyonu gibi değerleri yüklemiyor ... Bu nedenle, ihtiyaçlarınız için daha uygunsa maven-invoker-plugin'u kullanmanızı tavsiye ederim.

+2

'maven-plugin-testing-harness' nin gitmesi için uzun bir yol var gibi görünüyor. – EvgeniySharapov

+0

Oh, sanki öyle görünüyor, ama Maven 2.x'in ortaya çıkmasından bu yana bunun böyle olduğuna emin olalım, bu yüzden ... Bir süre önce (eğer varsa) ele alınabilir. – carlspring

+1

http://stackoverflow.com/questions/31528763/how-to-populate-parameter-defaultvalue-in-maven-abstractmojotestcase/36064396 adresinde ilgili bir yanıt var - yanıtın belirlendiği gibi görünen mojoRule.lookupConfiguredMojo() kullanılmasını önerir parametrelerde varsayılanlar. – wemu

İlgili konular