如何在不使用本地安装的工件的情况下构建

时间:2022-07-17 00:13:12

Is there any way to force Maven to use remote artifacts and not those installed on your machine? since I worry about runtime errors and not compilation errors build server is not valid option.

有没有办法强制Maven使用远程工件而不是机器上安装的工件?因为我担心运行时错误而不是编译错误构建服务器无效选项。

P.S. I know I could delete or rename the .m2 folder, but I bet there is some more clever way of doing this. Maybe some plugin or special command param?

附:我知道我可以删除或重命名.m2文件夹,但我敢打赌,有一些更聪明的方法可以做到这一点。也许一些插件或特殊命令参数?

2 个解决方案

#1


Having no local repository would mean your classpath consisting almost entirely of URLs on remote servers. I can't see why this would be supported as execution would be awful, and any dropped connection would result in classloader issues. Having a local repository ensures the jars are available before compilation/execution begins.

没有本地存储库意味着您的类路径几乎完全由远程服务器上的URL组成。我不明白为什么这会被支持,因为执行会很糟糕,任何丢弃的连接都会导致类加载器问题。拥有本地存储库可确保在编译/执行开始之前jar可用。

Also consider that WAR and EAR projects (and many using the dependency plugin) rely on downloading the jars to complete their packaging. There would be a huge overhead if these had to be retrieved from a remote repository on every build. I'm pretty sure the managers of central would not be keen on dealing with that load.

还要考虑WAR和EAR项目(以及许多使用依赖插件)依赖于下载jar来完成其包装。如果必须在每个构建中从远程存储库中检索这些开销,则会产生巨大的开销。我很确定*管理人员不会热衷于处理这种负担。

Some alternatives for you to consider:

一些替代方案供您考虑:

  • If you want to force a clean local repository on each build, you can use the purge goal of the dependency plugin.
  • 如果要在每个构建上强制使用干净的本地存储库,则可以使用依赖项插件的清除目标。

  • If you want to keep builds isolated, you can use separate Maven settings by passing -Dorg.apache.maven.global-settings=/path/to/global/settings.xml
  • 如果要保持构建隔离,可以通过将-Dorg.apache.maven.global-settings = / path /传递给/ global / settings.xml来使用单独的Maven设置

  • Alternatively you can override the local repository on a per build basis by passing -Dmaven.repo.local=/some/repo/path
  • 或者,您可以通过传递-Dmaven.repo.local = / some / repo / path来基于每个构建覆盖本地存储库

  • If you want to avoid hitting remote repositories on each build, add <updatePolicy>never</updatePolicy> to your remote repository configurations. This means Maven will only check for updates if you force it to with a "-U" switch on the command line
  • 如果要避免在每个构建*问远程存储库,请将 never 添加到远程存储库配置中。这意味着如果您在命令行上使用“-U”开关强制它,Maven将仅检查更新

  • If you want to take the latest version of a dependency, you can use the LATEST keyword in the version declaration (instead of the version number), though this can be risky if the dependency is incompatible.
  • 如果要使用最新版本的依赖项,可以在版本声明中使用LATEST关键字(而不是版本号),但如果依赖项不兼容,则可能存在风险。

  • If you want to take the current release version of a dependency, you can use the RELEASE keyword in the version declaration (instead of the version number). This is like LATEST, but tends to be the newest stable build, rather than the newest.
  • 如果要获取依赖项的当前发行版,可以在版本声明中使用RELEASE关键字(而不是版本号)。这就像LATEST,但往往是最新的稳定版本,而不是最新版本。

  • If you want to take the latest version of a dependency within a range, use Maven's version range notation, for example [1.0.0,2.0.0) means any version from 1.0.0 inclusive to 2.0.0 exclusive
  • 如果要在范围内采用最新版本的依赖项,请使用Maven的版本范围表示法,例如[1.0.0,2.0.0]表示从1.0.0到2.0.0的任何版本独占

For more details on LATEST and RELEASE, see section 9.3.1.3 of the Maven book.

有关LATEST和RELEASE的更多详细信息,请参阅Maven书的9.3.1.3节。

If you use an internal repository manager (obligatory Nexus and Artifactory references here), the overhead of purging the local repository is greatly reduced - you'll just have an increased local network traffic load.

如果您使用内部存储库管理器(此处必需的Nexus和Artifactory引用),则大大减少了清除本地存储库的开销 - 您只会增加本地网络流量负载。

#2


I don't think there's really a way to do what you are asking for. You could look into depending on SNAPSHOT releases (but that means changing your version string of the upstream projects to be SNAPSHOT versions).

我认为没有办法实现你所要求的。您可以根据SNAPSHOT版本进行调查(但这意味着将上游项目的版本字符串更改为SNAPSHOT版本)。

Incidentally, this was discussed at length in a recent Java Posse episode (#268). I don't think they ended up with a solution, but you may get some good ideas there.

顺便提一下,这是在最近的Java Posse剧集(#268)中详细讨论的。我不认为他们最终得到了解决方案,但你可能会在那里得到一些好主意。

I also like some of Rich Seller's ideas, which I'll be looking into myself.

我也喜欢Rich Seller的一些想法,我会自己调查一下。

#1


Having no local repository would mean your classpath consisting almost entirely of URLs on remote servers. I can't see why this would be supported as execution would be awful, and any dropped connection would result in classloader issues. Having a local repository ensures the jars are available before compilation/execution begins.

没有本地存储库意味着您的类路径几乎完全由远程服务器上的URL组成。我不明白为什么这会被支持,因为执行会很糟糕,任何丢弃的连接都会导致类加载器问题。拥有本地存储库可确保在编译/执行开始之前jar可用。

Also consider that WAR and EAR projects (and many using the dependency plugin) rely on downloading the jars to complete their packaging. There would be a huge overhead if these had to be retrieved from a remote repository on every build. I'm pretty sure the managers of central would not be keen on dealing with that load.

还要考虑WAR和EAR项目(以及许多使用依赖插件)依赖于下载jar来完成其包装。如果必须在每个构建中从远程存储库中检索这些开销,则会产生巨大的开销。我很确定*管理人员不会热衷于处理这种负担。

Some alternatives for you to consider:

一些替代方案供您考虑:

  • If you want to force a clean local repository on each build, you can use the purge goal of the dependency plugin.
  • 如果要在每个构建上强制使用干净的本地存储库,则可以使用依赖项插件的清除目标。

  • If you want to keep builds isolated, you can use separate Maven settings by passing -Dorg.apache.maven.global-settings=/path/to/global/settings.xml
  • 如果要保持构建隔离,可以通过将-Dorg.apache.maven.global-settings = / path /传递给/ global / settings.xml来使用单独的Maven设置

  • Alternatively you can override the local repository on a per build basis by passing -Dmaven.repo.local=/some/repo/path
  • 或者,您可以通过传递-Dmaven.repo.local = / some / repo / path来基于每个构建覆盖本地存储库

  • If you want to avoid hitting remote repositories on each build, add <updatePolicy>never</updatePolicy> to your remote repository configurations. This means Maven will only check for updates if you force it to with a "-U" switch on the command line
  • 如果要避免在每个构建*问远程存储库,请将 never 添加到远程存储库配置中。这意味着如果您在命令行上使用“-U”开关强制它,Maven将仅检查更新

  • If you want to take the latest version of a dependency, you can use the LATEST keyword in the version declaration (instead of the version number), though this can be risky if the dependency is incompatible.
  • 如果要使用最新版本的依赖项,可以在版本声明中使用LATEST关键字(而不是版本号),但如果依赖项不兼容,则可能存在风险。

  • If you want to take the current release version of a dependency, you can use the RELEASE keyword in the version declaration (instead of the version number). This is like LATEST, but tends to be the newest stable build, rather than the newest.
  • 如果要获取依赖项的当前发行版,可以在版本声明中使用RELEASE关键字(而不是版本号)。这就像LATEST,但往往是最新的稳定版本,而不是最新版本。

  • If you want to take the latest version of a dependency within a range, use Maven's version range notation, for example [1.0.0,2.0.0) means any version from 1.0.0 inclusive to 2.0.0 exclusive
  • 如果要在范围内采用最新版本的依赖项,请使用Maven的版本范围表示法,例如[1.0.0,2.0.0]表示从1.0.0到2.0.0的任何版本独占

For more details on LATEST and RELEASE, see section 9.3.1.3 of the Maven book.

有关LATEST和RELEASE的更多详细信息,请参阅Maven书的9.3.1.3节。

If you use an internal repository manager (obligatory Nexus and Artifactory references here), the overhead of purging the local repository is greatly reduced - you'll just have an increased local network traffic load.

如果您使用内部存储库管理器(此处必需的Nexus和Artifactory引用),则大大减少了清除本地存储库的开销 - 您只会增加本地网络流量负载。

#2


I don't think there's really a way to do what you are asking for. You could look into depending on SNAPSHOT releases (but that means changing your version string of the upstream projects to be SNAPSHOT versions).

我认为没有办法实现你所要求的。您可以根据SNAPSHOT版本进行调查(但这意味着将上游项目的版本字符串更改为SNAPSHOT版本)。

Incidentally, this was discussed at length in a recent Java Posse episode (#268). I don't think they ended up with a solution, but you may get some good ideas there.

顺便提一下,这是在最近的Java Posse剧集(#268)中详细讨论的。我不认为他们最终得到了解决方案,但你可能会在那里得到一些好主意。

I also like some of Rich Seller's ideas, which I'll be looking into myself.

我也喜欢Rich Seller的一些想法,我会自己调查一下。