Home > Blockchain >  Using plugin and execution steps based on Profiles
Using plugin and execution steps based on Profiles

Time:11-23

I have a pom as below

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>X.0.0</modelVersion>
    <parent>
        <groupId>asdasd</groupId>
        <artifactId>asdasd</artifactId>
        <version>0.334.0-SNAPSHOT</version>
        <relativePath>../something/pom.xml</relativePath>
    </parent>

    <!-- ======================================================================= -->
    <!-- P R O J E C T                                                           -->
    <!-- ======================================================================= -->
    <artifactId>dfdfd</artifactId>
    <name>xxxxx</name>
    <packaging>content-package</packaging>

    <properties>
        <sonar.sources>${basedir}/src/main/angular/src</sonar.sources>
        <sonar.tests>${basedir}/src/main/angular/src</sonar.tests>
    </properties>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/content/jcr_root</directory>
                <excludes>
                    <exclude>**/*.iml</exclude>
                </excludes>
                <targetPath>jcr_root</targetPath>
            </resource>
            <resource>
                <filtering>true</filtering>
                <directory>${basedir}/src/main/content/META-INF/xxx/definition</directory>
                <targetPath>../xxx/META-INF/vault/definition</targetPath>
            </resource>
        </resources>
        <sourceDirectory>${basedir}/src/main/content/jcr_root</sourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>xml-maven-plugin</artifactId>
            </plugin>

            <!-- start frontend -->
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.8.0</version>
                <inherited>false</inherited>

                <configuration>
                    <nodeVersion>v16.17.0</nodeVersion>
                    <npmVersion>8.15.0</npmVersion>
                    <nodeDownloadRoot>xxxxx</nodeDownloadRoot>
                    <npmDownloadRoot>xxxxx</npmDownloadRoot>
                    <installDirectory>src/main/angular</installDirectory>
                    <workingDirectory>src/main/angular</workingDirectory>
                </configuration>

                <executions>

                    <execution>
                        ......
                    </execution>

                    <execution>
                        <id>npm rebuild node-sass</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>rebuild node-sass</arguments>
                        </configuration>
                    </execution>

                    <execution>
                        <id>npm run e2e</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>run e2e</arguments>
                        </configuration>
                    </execution>

                </executions>
            </plugin>
            <!-- end frontend -->
        </plugins>
    </build>


</project>

I want to run execution step with id npm run e2e based on profile. What I have in mind is 2 profiles default and e2eTests. With default, I can set activeByDefault as true and skip the e2e test. And when I pass e2eTests using -P , I should be able to run all execution including id=npm run e2e

I tried to move them into <profiles> but then the <sourceDirectory> was complaining. I found some links like this for it, but the problem comes on how to access plugins when I have <profiles> outside of <build>

I am not able to structure it properly.

CodePudding user response:

Normally, it is not required to copy entire <build> configuration into profile definition - it should be enough to just define extra plugin execution, smth. like that:

  <profiles>
    <profile>
      <id>e2eTests</id>
      <build>
        <plugins>
          <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.8.0</version>
            <executions>
              <execution>
                <id>npm run e2e</id>
                <goals>
                  <goal>npm</goal>
                </goals>
                <configuration>
                  <arguments>run e2e</arguments>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

However, I would configure that in different way:

  1. either bind execution of e2e tests to the lifecycle phase designed for that (integration-test) - in this case mvn package won't trigger e2e tests:
...
<execution>
  <id>npm run e2e</id>
  <phase>integration-test</phase>
  <goals>
    <goal>npm</goal>
  </goals>
  <configuration>
    <arguments>run e2e</arguments>
  </configuration>
</execution>
...
  1. or take advantage of properties and profiles, for example:
<properties>
  ...
  <skip.e2e>true</skip.e2e>
  ...
</properties>

<profiles>
  <profile>
    <id>e2eTests</id>
    <properties>
      <skip.e2e>false</skip.e2e>
    </properties>
  </profile>
</profiles>
...
<execution>
  <id>npm run e2e</id>
  <phase>integration-test</phase>
  <goals>
    <goal>npm</goal>
  </goals>
  <configuration>
    <skip>${skip.e2e}</skip>
    <arguments>run e2e</arguments>
  </configuration>
</execution>
...
  • Related