我正在使用 Maven 来构建一个特定的项目,并且在 POM 中,我正在使用 Maven Shade 插件构建主要工件的 3 个不同变体(我正在使用所包含的日志框架的各种组合来创建 uber jar)。阴影插件使用替代工件 ID 及其各自的依赖减少的 poms 创建 jar。
我现在的挑战是如何将这些新工件部署到我的远程存储库。我正在使用 Maven 安装插件将它们安装到我的本地存储库,但 Maven 部署插件需要显式配置存储库 URL。我想要发生的是让插件采用默认部署使用的任何远程存储库,无论是快照或发布存储库还是我通过命令行传入的另一个存储库 URL。我希望找到一些像 ${project.remoterepo.url} 这样的 Maven 属性,它相当于已解析的存储库。当部署目标已经执行此操作时,必须显式配置远程 URL 似乎很愚蠢。
任何建议表示赞赏。谢谢!
这就是我根据版本模式自动选择 SNAPSHOT 或 RELEASE 重现的方法:(我知道这是一种代码味道,但就 ASF 而言,无论出于什么原因,我都不愿意包含您的代码)可以解决我的要求)
<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>
Tiemo Vorschütz 方法是个好主意,但可能不适合我。 它会出现一些“线程“主”BUG 中的异常!源单元“脚本”错误中“转换”阶段的异常。
我已将 gmaven 插件更改为较新版本并修复了错误,将其从 'gmaven-plugin' 1.x 更改为 'groovy-maven-plugin' 2.x 像这样:
<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>
实现此目的的一个简单方法是利用
distributionManagement
和 build-helper-maven-plugin
,正如 Tiemo Vorschütz 的答案所指出的那样。
...
<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>
...
使用
maven-antrun-plugin
解析存储库 ID/URL 的另一种方法:
<distributionManagement>
<snapshotRepository>
<id>maven-snapshots</id>
<url>https://example.com/maven-snapshots</url>
</snapshotRepository>
<repository>
<id>maven-releases</id>
<url>https://example.com/maven-releases</url>
</repository>
</distributionManagement>
<build>
<plugins>
<!-- Resolve the correct repository ID/URL based on the 'project.version' property and -->
<!-- export them as the 'resolvedRepositoryId' and 'resolvedRepositoryUrl' properties -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<id>resolve-repository</id>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<exportAntProperties>true</exportAntProperties>
<target>
<condition property="resolvedRepositoryId" value="${project.distributionManagement.snapshotRepository.id}" else="${project.distributionManagement.repository.id}">
<contains string="${project.version}" substring="-SNAPSHOT"/>
</condition>
<condition property="resolvedRepositoryUrl" value="${project.distributionManagement.snapshotRepository.url}" else="${project.distributionManagement.repository.url}">
<contains string="${project.version}" substring="-SNAPSHOT"/>
</condition>
<echo message="Resolved repository: ${resolvedRepositoryId}:${resolvedRepositoryUrl}"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
<!-- Deploy using the 'resolvedRepositoryId' and 'resolvedRepositoryUrl' properties -->
<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>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<file>${project.build.directory}/<!--your-file--></file>
<repositoryId>${resolvedRepositoryId}</repositoryId>
<url>${resolvedRepositoryUrl}</url>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>