1

I have a POM file which includes a property (under properties section) which has an IP value that we use when pushing it to git.

<device.ip>1.2.3.4</device.ip>

But for my builds, I need to use a another IP value, so I should change it to my required IP when I start to work on a new branch.

I'd like to be able to check the variable value at build startup and abort it given the variable value is different from what I need.

Any other solutions also welcomed.

(I hope my question would not be downgraded because of lack of code - there is no really code to write here. The scenario is quite self explanatory)

Than you for your advice.

dushkin
  • 1,939
  • 3
  • 37
  • 82
  • 1
    You could use the AntRun plugin to check the property, then call the Fail Task if your condition is met. https://maven.apache.org/guides/mini/guide-using-ant.html https://ant.apache.org/manual/Tasks/fail.html – Momo Dec 31 '18 at 07:37
  • 1
    U can use Build Number plugin to do so. https://stackoverflow.com/a/38163969/4953620 just confogure you IP depending on your situation. – Saidolim Dec 31 '18 at 07:38
  • Why is the ip related to your git push? – khmarbaise Dec 31 '18 at 08:02
  • @khmarbaise The ip is configured in pom and it contains the production device ip. I need a development device for my work. – dushkin Dec 31 '18 at 08:50
  • Is the IP predictable based on development and production environment ? – khmarbaise Dec 31 '18 at 11:04
  • @khmarbaise Sorry, not following you... The device on production has a constant IP and in Dev another constant IP. Please explain :) – dushkin Dec 31 '18 at 11:08
  • This already answers my question. This means you have an IP for production and a different IP for development. This means you have handle the different IP's based on a profile..Or is there an identification (hostname something else?) by which you can identify the environment? – khmarbaise Dec 31 '18 at 15:01

2 Answers2

2

You could use the maven-enforcer-plugin which support such checks.

The usage looks like this for the requirePropery rule.

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M2</version>
        <executions>
          <execution>
            <id>enforce-property</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireProperty>
                  <property>device.ip</property>
                  <message>You must set a device.ip property!</message>
                  <regex>.*\d.*</regex> <!-- Express the value you need. -->
                  <regexMessage>The device.ip property contain...</regexMessage>
                </requireProperty>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Your suggested plugin is great, but eventually won't help me so much because I'll have to set my dev IP as the regex tag value, and anyways I'll have to modify it again to the production IP before each push... Is there a way to solve it? – dushkin Dec 31 '18 at 10:09
  • guess some external value should be involved somehow – dushkin Dec 31 '18 at 11:01
2

I would suggest splitting your project in modules.

  • Module 1 contains your code without any configuration.
  • Module 2 refers to module 1 and additionally contains what goes into production. For multiple deployments create an additional module for each. This is where your production property goes.
  • Module 3 refers to module 1 (but not 2) and contains whatever you need for development (configuration like this property and helper classes). For complex scenarios make an additional module for each.

This has worked well for me.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347