6

I am using Maven to build a particular project, and within the POM I am building 3 different variants of the primary artifact using the maven shade plugin (I'm creating uber jars with various combinations of included logging frameworks). The shade plugin creates jars with alternative artifact IDs and their respective dependency-reduced-poms.

My challenge is now how to deploy these new artifacts to my remote repositories. I'm using the maven install plugin to install them to my local repo, but the maven deploy plugin requires explicit configuration of a repository URL. What I want to happen is for the plugin to adopt whichever remote repo the default deploy uses, whether its the snapshot or release repo or another repo URL that I pass in via command-line. I was hoping to find some maven property like ${project.remoterepo.url} that equated to the resolved repo. It seems silly to have to explicitly configure the remote URL when the deploy goal already does this.

Any advice appreciated. Thanks!

Richard Sand
  • 642
  • 6
  • 20
  • Did you ever find out how to do this? I'm encountering the same problem and deploy:deploy-file is starting to infuriate me. – Thomas Kåsene Oct 05 '17 at 14:15
  • 1
    No, and the maven kings are very unsympathetic. You may only use the tools as they decree! Change your business to accommodate the tool is basically what I was told – Richard Sand Oct 05 '17 at 18:48

3 Answers3

1

This is what I did to automatically select either the SNAPSHOT or RELEASE repro based on the version pattern: (I know that this is a kind of code smell but as far as the ASF is not willing to include your code for what ever reason I could solve my requirement)

<properties>
    <deploy.repositoryUrl>.. url release repo ..</deploy.repositoryUrl>
    <deploy.repositoryId>.. id release repo ..</deploy.repositoryId>
    <deploy.repositorySnapshotUrl>.. snapshot repo ..</deploy.repositorySnapshotUrl>
    <deploy.repositorySnapshotId>.. id snapshot repo ..</deploy.repositorySnapshotId>       
</properties>

<build>     
    <plugins>   
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <!-- sets the isSnapshot property to true if SNAPSHOT was used -->
                    <id>build-helper-regex-is-snapshot-used</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>isSnapshot</name>
                        <value>${project.version}</value>
                        <regex>.*-SNAPSHOT</regex>
                        <replacement>true</replacement>
                        <failIfNoMatch>false</failIfNoMatch>                            
                    </configuration>
                </execution>                    
            </executions>
        </plugin>   
        <!-- set the properties deploy.Url and deploy.Id during validation to 
        either the snapshot repository or the release repository 
        depending on the version pattern.-->            
        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source><![CDATA[
                            pom.properties['deploy.Url']=pom.properties['isSnapshot'].equals('true') ? pom.properties['deploy.repositorySnapshotUrl'] : pom.properties['deploy.repositoryUrl'];
                            pom.properties['deploy.Id']=pom.properties['isSnapshot'].equals('true') ? pom.properties['deploy.repositorySnapshotId'] : pom.properties['deploy.repositoryId'];
                        ]]></source>
                    </configuration>
                </execution>
            </executions>
        </plugin>   
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>3.0.0-M1</version>
            <configuration>
                <skip>true</skip>
            </configuration> 
            <executions>
                <execution>
                    <id>DeployToArtifactory</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy-file</goal>
                    </goals>
                    <configuration>
                        <url>${deploy.Url}</url>
                        <repositoryId>${deploy.Id}</repositoryId>
                        <file>target/${project.build.finalName}.${project.packaging}</file>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${project.version}</version>
                        <packaging>jar</packaging>
                        <classifier>resources</classifier>
                        <pomFile>${project.build.directory}/pom/pom.xml</pomFile>
                    </configuration>
                </execution>
            </executions>                                                        
        </plugin>
    </plugins>
</build>
0

Tiemo Vorschütz method is a good idea, but may not work for me. It case some 'Exception in thread "main" BUG! exception in phase 'conversion' in source unit 'script' errors.

I have changed the gmaven plugin to a newer version and fixed errors, change it from 'gmaven-plugin' 1.x to 'groovy-maven-plugin' 2.x like this:

<properties>
<deploy.repositoryUrl>.. url release repo ..</deploy.repositoryUrl>
<deploy.repositoryId>.. id release repo ..</deploy.repositoryId>
<deploy.repositorySnapshotUrl>.. snapshot repo ..</deploy.repositorySnapshotUrl>
<deploy.repositorySnapshotId>.. id snapshot repo ..</deploy.repositorySnapshotId>       
            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <!-- sets the isSnapshot property to true if SNAPSHOT was used -->
                    <id>build-helper-regex-is-snapshot-used</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>isSnapshot</name>
                        <value>${project.version}</value>
                        <regex>.*-SNAPSHOT</regex>
                        <replacement>true</replacement>
                        <failIfNoMatch>false</failIfNoMatch>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- set the properties deploy.Url and deploy.Id during validation to
        either the snapshot repository or the release repository
        depending on the version pattern.-->
        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>groovy-maven-plugin</artifactId>
            <version>2.1.1</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source><![CDATA[
                            project.getProperties().put('deploy.Url',properties['isSnapshot'].equals('true') ? properties['deploy.repositorySnapshotUrl'] : properties['deploy.repositoryUrl']);
                            project.getProperties().put('deploy.Id',properties['isSnapshot'].equals('true') ? properties['deploy.repositorySnapshotId'] : properties['deploy.repositoryId']);
                            ]]></source>
                    </configuration>
                </execution>
            </executions>
        </plugin>        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>3.0.0-M1</version>
        <configuration>
            <skip>true</skip>
        </configuration> 
        <executions>
            <execution>
                <id>DeployToArtifactory</id>
                <phase>deploy</phase>
                <goals>
                    <goal>deploy-file</goal>
                </goals>
                <configuration>
                    <url>${deploy.Url}</url>
                    <repositoryId>${deploy.Id}</repositoryId>
                    <file>target/${project.build.finalName}.${project.packaging}</file>
                    <groupId>${project.groupId}</groupId>
                    <artifactId>${project.artifactId}</artifactId>
                    <version>${project.version}</version>
                    <packaging>jar</packaging>
                    <classifier>resources</classifier>
                    <pomFile>${project.build.directory}/pom/pom.xml</pomFile>
                </configuration>
            </execution>
        </executions>                                                        
    </plugin>
0

A simple way to do this is leveraging distributionManagement and build-helper-maven-plugin as pointed our by Tiemo Vorschütz's answer.

...

<distributionManagement>
  <repository>
    <id>my-release</id>
    <name>my-release</name>
    <url>https://example.com/release</url>
  </repository>
  <snapshotRepository>
    <id>my-snapshot</id>
    <name>my-snapshot</name>
    <url>https://example.com/snapshot</url>
  </snapshotRepository>
</distributionManagement>

...

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>3.3.0</version>
      <executions>
        <execution>
          <!-- sets the repoUrl property to the correct repository depending on the type of version -->
          <id>build-deploy-url</id>
          <phase>validate</phase>
          <goals>
            <goal>regex-property</goal>
          </goals>
          <configuration>
            <name>repoUrl</name>
            <value>${project.version}</value>
            <regex>.*-SNAPSHOT</regex>
            <replacement>${project.distributionManagement.snapshotRepository.url}</replacement>
            <failIfNoMatch>${project.distributionManagement.repository.url}</failIfNoMatch>
          </configuration>
        </execution>
      </executions>
    </plugin>

    <plugin>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>3.0.0</version>
      <executions>
        <execution>
          <id>deploy-file</id>
          <phase>deploy</phase>
          <goals>
            <goal>deploy-file</goal>
          </goals>
          <configuration>
            <file>${project.build.directory}/<!--your-file--></file>
            <url>${repoUrl}</url>
            <repositoryId><!--repo as per settings.xml if credentials are the same--></repositoryId>
            <groupId>${project.groupId}</groupId>
            <artifactId>${project.artifactId}</artifactId>
            <version>${project.version}</version>
            <packaging><!--your packaging--></packaging>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

...
acm
  • 2,086
  • 3
  • 16
  • 34