如何从离线计算机构建和发布 Maven 项目(将文件目录作为 lib-repository)



我需要使用 Jenkins 构建和发布项目, 在无法访问Maven Central的服务器上,甚至无法访问Nexus。

鉴于我可以访问开发机器上的 maven-central, 为了填补专家local_repository,我可以做

mvn dependency:resolve-plugins dependency:go-offline

以复制 Linux 服务器上的local_repository。

然后,为了克服Non-resolvable parent POM错误, 如此处所述,我用伪造的中央配置文件填充了Windows(dev)和Linux(Jenkins)的特定配置文件,以覆盖我的父Pom所做的Maven-Central引用:

<profiles>
<profile>
<id>windows</id>
<activation>              
<os>
<family>Windows</family>
</os>
</activation>
<properties>                <repository.base.url>file:///c:/maven_distribution_repo/</repository.base.url>
</properties>           
</profile>
<profile>
<id>linux</id>
<activation>              
<os>
<family>Linux</family>
</os>
</activation>
<properties>
<repository.base.url>file:///appli/Maven_3.1.1_build/maven_distribution_repo/</repository.base.url>         
</properties>           
<repositories>
<repository>
<id>central</id>
<name>Maven Plugin Repository</name>
<!--<url>http://repo1.maven.org/maven2</url>-->
<url>${repository.base.url}</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>   
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Maven Plugin Repository</name>
<!--<url>http://repo1.maven.org/maven2</url>-->
<url>${repository.base.url}</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories> 
</profile>
</profiles>

这样,mvn -o compile仍然会引发Non-resolvable parent POM错误! 但是使用此处建议的--legacy-local-repository选项, 设法通过使用本地存储库伪造远程存储库,Non-resolvable parent POM问题消失了:

MVN --legacy-local-repository compile

尽管如此,还是出现了一个奇怪的错误(在这里描述):

[ERROR] Failed to execute goal on project myProject: Could not resolve dependencies for project some.package:myProject:war:0.0.7-SNAPSHOT: Failed to collect dependencies at org.jxls:jxls-poi:jar:1.0.11 -> org.jxls:jxls:jar:[2.0.0,):  org.jxls:jxls:jar:[2.0.0,) within specified range -> [Help 1]

但它隐藏了一个早期的警告:

[WARNING] Could not transfer metadata org.jxlsjxls/maven-metadata.xml from/to central (/path):/appli/Maven_3.1.1_build/maven_distribution_repo/org/jxls/jxls/maven-metadata-central.xml (access forbidden)

使用 --legacy-local-repository,maven 似乎使用分发存储库路径作为本地库存储库!

我在绒球中交换了它们:

<profile>
<id>linux</id>
<activation>              
<os>
<family>Linux</family>
</os>
</activation>
<properties>    
<!--<repository.base.url>file:///appli/Maven_3.1.1_build/maven_distribution_repo/</repository.base.url>-->
<repository.base.url>file:///appli/Maven-3.1.1_build/maven_local_repo/</repository.base.url>
</properties>
...

并且还必须复制到本地存储库中: 所有maven-metadata-maven2_central.xmlmaven-metadata.xml

使用以下 bash 命令:

for file in $(find /appli/Maven-3.1.1_build/maven_local_repo -type f -name 'maven-metadata-maven2_central.xml'); do cp $file $(echo ${file%/*}/maven-metadata.xml); done

和。。。宾果游戏!

构建成功

在后期阶段,release:perform到本地 lib 存储库中似乎很奇怪。

你会有更好的而不是痛苦的解决方案吗?

Maven 部署插件可以解决这个问题。

mvn deploy -DaltDeploymentRepository=local-temp::default::file://directory/

更详尽的示例:


# 1. Create temporary folder
tmp_repo=$(mktemp -d systemds-repo-tmp-XXXXX)

# 2. Create a simple settings.xml
cat <<EOF >../tmp-settings-nexus.xml
<settings>
<activeProfiles>
<activeProfile>local-temp</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>local-temp</id>
<repositories>
<repository>
<id>local-temp</id>
<url>${tmp_repo}</url>
</repository>
</repositories>
</profile>
</profiles>
</settings>
EOF

# 3. deploy to local
mvn --settings ../tmp-settings-nexus.xml -Pdistribution deploy 
-DaltDeploymentRepository=local-temp::default::file://${tmp_repo} 
-Daether.checksums.algorithms='SHA-512,SHA-1,MD5'

您应该考虑设置一个本地存储库(例如,Nexus),您的构建计算机可以访问该存储库。

这是一个非常常见的设置。

它使你可以在没有互联网连接的情况下构建构建计算机。

最新更新