Can I force Maven 2 to require a property to be specified on the command line?

后端 未结 4 1044
被撕碎了的回忆
被撕碎了的回忆 2020-12-31 02:19

I\'m setting up a maven build, and the destination server needs to be specified on the command line as a property (which is then used to select the appropriate profile), eg<

相关标签:
4条回答
  • 2020-12-31 02:44

    To elaborate on edbrannin's alternate solution:

    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.yourcompany</groupId>
      <artifactId>yourproject</artifactId>
      <version>1.0-SNAPSHOT</version>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>checkParam</id>
                <phase>initialize</phase>
                <goals><goal>run</goal></goals>
                <configuration>
                  <tasks>
                    <fail message="'env' property must be set" unless="env"/>
                  </tasks>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    

    will give you the following output:

    [INFO] ------------------------------------------------------------------------
    [ERROR] BUILD ERROR
    [INFO] ------------------------------------------------------------------------
    [INFO] An Ant BuildException has occured: 'env' property must be set
    

    IMHO the most straightforward way to do it (the one I'd go for personally).

    You could even control a set of allowed values using a nested <condition> containing <or> and <equals> tags (see the Ant manual: http://ant.apache.org/manual/Tasks/conditions.html)

    0 讨论(0)
  • 2020-12-31 02:48

    My first inclination would be to make a Profile that's active whenever the env property is unset, and have it fail somehow. Perhaps you could write a Maven Plugin that tests for that property and fails if it's not present?

    Alternately, you could probably test for it with a very small ant-build script.

    0 讨论(0)
  • 2020-12-31 02:59

    Maybe you could use such a workaround: in Maven you can activate a profile if some property is not set:

    <project>
    ...
        <profiles>
            <profile>
                <id>failure_profile</id>
                <activation>
                    <property>
                        <name>!env</name>
                    </property>
                </activation>
            </profile>
        </profiles>
    </project>
    

    Then you shall force this profile always fail, e.g. using maven-enforcer-plugin:

    <profile>
        <id>failure_profile</id>
        ...
        <build>
            <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <executions>
                  <execution>
                    <id>enforce</id>
                    <goals>
                      <goal>enforce</goal>
                    </goals>
                    <configuration>
                      <rules>
                        <AlwaysFail/>
                      </rules>
                      <fail>true</fail>
                    </configuration>
                  </execution>
                </executions>
              </plugin>
            </plugins>
          </build>
    </profile>
    

    If you don't provide -Denv build will then fail:

    [INFO] [enforcer:enforce {execution: enforce}]
    [WARNING] Rule 0: org.apache.maven.plugins.enforcer.AlwaysFail failed with message:
    Always fails!
    [INFO] ---------------------------------------------------------
    [ERROR] BUILD ERROR
    

    Well, it's much more verbose then Ant, but pure Maven :)

    0 讨论(0)
  • 2020-12-31 03:01

    Everyone is close, but there's a rule in the enforcer to specifically check for a property, no ant or funky profiles required: http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html

    The rule can also check the value of the property.

    0 讨论(0)
提交回复
热议问题