On 8/1/2023 7:42 PM, Garret Wilson wrote:
Now the child POMs can turn off deployment by simply setting `maven.deploy.skip` to `false`, and kill two birds with one stone: deployment will be disabled whether the Nexus Staging Plugin or the Maven Deploy Plugin was used.

In my previous message I explained how to set up the Nexus Staging Plugin so that it can easily be disabled in child projects (along with the Maven Build Plugin) using one simple property: `maven.deploy.skip`. Now for the next step, the grand finale.

In my original question, I asked if there was a way to keep the Nexus Staging Plugin from being inherited in child POMs by default. Really "inherited" is an implementation detail. What I really wanted to know is if there was to configure the Nexus Staging Plugin to be enabled by default in the parent POM, but be disabled by default in the child POM. The semantic distinction is subtle, but important; it turns out that there's no way to turn off inheritance, but with a few tricks (i.e. quite a few very ugly hacks), we can change the default configuration based upon whether the plugin is being invoked in the parent POM (here `com.globalmentor:globalmentor-root`) or from some child POM.

If Maven supported expressions (oh, if only), this would be rather straightforward. Since it doesn't we'll use `org.codehaus.mojo:build-helper-maven-plugin` to do things that no one should ever have to do (or even consider) in a POM; it looks like this:

```xml
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>set-is-skip-deploy-false-or-prefixed</id>
      <phase>validate</phase>
      <goals>
        <goal>regex-property</goal>
      </goals>
      <configuration>
        <name>maven.deploy.skip</name>
<value>_${project.groupId}_${project.artifactId}</value>
<regex>^_com\.globalmentor_globalmentor-root$</regex>
        <replacement>false</replacement>
        <failIfNoMatch>false</failIfNoMatch>
      </configuration>
    </execution>
    <execution>
      <id>set-is-skip-deploy</id>
      <phase>initialize</phase>
      <goals>
        <goal>regex-property</goal>
      </goals>
      <configuration>
        <name>maven.deploy.skip</name>
        <value>${maven.deploy.skip}</value>
        <regex>^_.*</regex>
        <replacement>true</replacement>
        <failIfNoMatch>false</failIfNoMatch>
      </configuration>
    </execution>
  </executions>
</plugin>
```

I won't explain here what's going on; I intend to write a blog post about it some day. The end result is that if the `com.globalmentor:globalmentor-root` POM is in effect, the `maven.deploy.skip` property is set to `true`; for any other descendant project, the `maven.deploy.skip` property is set to `false`. It works like this pseudocode:

```xml
<properties>
<maven.deploy.skip>$("${project.groupId}:${project.artifactId}}"!="com.globalmentor:globalmentor-root)</maven.deploy.skip>
```

(Wouldn't it be nice to have expressions like this.)

I succeeded in pulling off effectively what I asked for. But in reality I would like the `maven.deploy.skip` property to be determined by whether `nexus.host` is set to some value, and I want `nexus.host` to be set to a default value in `com.globalmentor:globalmentor-root` but not in child POMs. But that sort of two-layered logic (although simple if Maven supported expressions) is too much for the ugly kludge I'm using (and more than my mind wants to deal with).

So in the end I settled for a sort of compromise: I made the value of `maven.deploy.skip` dependent on whether `nexus.host` is set to a non-empty string. Thus the Nexus Staging Plugin is enabled by default, but in any child POM I can easily turn off deployment by setting the `nexus.host` to the empty string, like this:

```xml
<properties>
  <nexus.host></nexus.host>
```

This automatically turns off `maven.deploy.skip` via this ugly kludge:

```xml
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <executions>
    <execution>
      <id>set-is-skip-deploy-true-or-prefixed</id>
      <phase>validate</phase>
      <goals>
        <goal>regex-property</goal>
      </goals>
      <configuration>
        <name>maven.deploy.skip</name>
        <value>_${nexus.host}</value>
        <regex>^_$</regex>
        <replacement>true</replacement>
        <failIfNoMatch>false</failIfNoMatch>
      </configuration>
    </execution>
    <execution>
      <id>set-is-skip-deploy</id>
      <phase>initialize</phase>
      <goals>
        <goal>regex-property</goal>
      </goals>
      <configuration>
        <name>maven.deploy.skip</name>
        <value>${maven.deploy.skip}</value>
        <regex>^_.*</regex>
        <replacement>false</replacement>
        <failIfNoMatch>false</failIfNoMatch>
      </configuration>
    </execution>
  </executions>
</plugin>
```

I'll live with this for now. I'll probably wind up writing a Maven set-property-from-expression plugin at some point, when I can't digest this sort of nasty workaround any more.

Garret


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@maven.apache.org
For additional commands, e-mail: users-h...@maven.apache.org

Reply via email to