简体   繁体   中英

How to make non-profiled maven build fail?

I would agree that this question is somewhat weird.

Let's say I have a maven project which has following profiles.

$ mvn help:all-profiles
Profile Id: profile1 (...)
Profile Id: profile2 (...)
Profile Id: profile3 (...)
$

Is there anyway that I can force to fail without any specific profile selection?

$ mvn clean package
FAIL
$ mvn -Pprofile1 clean package
SUCCESS
$ mvn -Pprofile2 clean package
SUCCESS
$ mvn -Pprofile3 clean package
SUCCESS
$

UPDATE(with personal conclusion)

Again, this question is not a good one even if there would be someone who has the exact problem.

The original intention is charging the responsibility who build the project with explicit profile selection.

Both the comment of @khmarbaise and the answer of @AngeloNeuschitzer are OK to solve the problem.

But here I think I should update what I ended up with.

As @khmarbaise commented, we can do like this.

  • Make profiles
  • Make one of those profiles activeByDefault
$ mvn help:all-profiles
Profile Id: development
Profile Id: integration
Profile Id: staging
Profile Id: production (active by default)
$ mvm clean package
Profile -> production
$ mvn -Pdevelopment clean package
Profile -> development

One of original problem that I faced is what if anyone build for development and deploy to production, accidentally, by fault?

I choose the production profile active by default for this case. Using the classifier is one of additional method I can use.

Add a profile that does something that has to fail on clean and make it activeByDefault . You may even want to write a plugin that does nothing but fail on you for this.

CAREFUL ! I wrote this without making certain that the directory '/' is really undeleteable in all circumstances. This could result in severe data loss , use at you own risk. (It works on my machine, though)

    <profile>
        <id>failure</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <directory>#!?:</directory>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-clean-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>clean</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <failOnError>true</failOnError>
                        <filesets>
                            <fileset>
                                <directory>/</directory>
                            </fileset>
                        </filesets>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

You can use the maven-enforcer-plugin to fail a build when no profile is active. It's much cleaner than having a special profile that always fails (the code in the accepted answer even looks kind of malicious).

Use the build-in requireActiveProfile rule with <all>false</all> to enforce that at least one profile is active:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M2</version>
        <executions>
          <execution>
            <id>enforce-first-or-second-profile-is-active</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireActiveProfile>
                  <profiles>first,second</profiles>
                  <all>false</all>
                </requireActiveProfile>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

You can even add a custom message in the configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM