Maven:如何检查工件是否存在?

发布于 2024-10-14 14:15:50 字数 94 浏览 3 评论 0原文

如何从 Mojo 内部检查本地存储库中是否已存在工件?

我正在将大型二进制文件安装到本地 Maven 存储库中,在尝试下载它们之前我需要知道它们是否已经存在。

How do I check from within a Mojo if an artifact already exists in the local repository?

I'm installing large binaries into the local Maven repository and I need to know if they already exist before attempting to download them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

—━☆沉默づ 2024-10-21 14:15:50

http://docs.codehaus.org/display/MAVENUSER 的帮助下解决/Mojo+Developer+Cookbook

/**
 * The local maven repository.
 *
 * @parameter expression="${localRepository}"
 * @required
 * @readonly
 */
@SuppressWarnings("UWF_UNWRITTEN_FIELD")
private ArtifactRepository localRepository;
/**
 * @parameter default-value="${project.remoteArtifactRepositories}"
 * @required
 * @readonly
 */
private List<?> remoteRepositories;
/**
 * Resolves Artifacts in the local repository.
 * 
 * @component
 */
private ArtifactResolver artifactResolver;
/**
 * @component
 */
private ArtifactFactory artifactFactory;
[...]
Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, packagingType, classifier);
boolean artifactExists;
try
{
  // Downloads the remote artifact, if necessary
  artifactResolver.resolve(artifact, remoteRepositories, localRepository);
  artifactExists = true;
}
catch (ArtifactResolutionException e)
{
  throw new MojoExecutionException("", e);
}
catch (ArtifactNotFoundException e)
{
  artifactExists = false;
}
if (artifactExists)
  System.out.println("Artifact found at: " + artifact.getFile());

如果您想检查远程工件是否存在而不下载它,您可以使用 Aether 库 执行以下操作(基于 http ://dev.eclipse.org/mhonarc/lists/aether-users/msg00127.html):

MavenDefaultLayout defaultLayout = new MavenDefaultLayout();
RemoteRepository centralRepository = new RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2/").build();
URI centralUri = URI.create(centralRepository.getUrl());
URI artifactUri = centralUri.resolve(defaultLayout.getPath(artifact));
HttpURLConnection connection = (HttpURLConnection) artifactUri.toURL().openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
boolean artifactExists = connection.getResponseCode() != 404;

具有以下依赖项:org.eclipse.aether:aether-util:0.9.0.M2 和以下导入:

import java.net.HttpURLConnection;
import java.net.URI;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.repository.layout.MavenDefaultLayout;

Solved with the help of http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook

/**
 * The local maven repository.
 *
 * @parameter expression="${localRepository}"
 * @required
 * @readonly
 */
@SuppressWarnings("UWF_UNWRITTEN_FIELD")
private ArtifactRepository localRepository;
/**
 * @parameter default-value="${project.remoteArtifactRepositories}"
 * @required
 * @readonly
 */
private List<?> remoteRepositories;
/**
 * Resolves Artifacts in the local repository.
 * 
 * @component
 */
private ArtifactResolver artifactResolver;
/**
 * @component
 */
private ArtifactFactory artifactFactory;
[...]
Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, packagingType, classifier);
boolean artifactExists;
try
{
  // Downloads the remote artifact, if necessary
  artifactResolver.resolve(artifact, remoteRepositories, localRepository);
  artifactExists = true;
}
catch (ArtifactResolutionException e)
{
  throw new MojoExecutionException("", e);
}
catch (ArtifactNotFoundException e)
{
  artifactExists = false;
}
if (artifactExists)
  System.out.println("Artifact found at: " + artifact.getFile());

If you want to check if a remote artifact exists without downloading it, you can use the Aether library to do the following (based on http://dev.eclipse.org/mhonarc/lists/aether-users/msg00127.html):

MavenDefaultLayout defaultLayout = new MavenDefaultLayout();
RemoteRepository centralRepository = new RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2/").build();
URI centralUri = URI.create(centralRepository.getUrl());
URI artifactUri = centralUri.resolve(defaultLayout.getPath(artifact));
HttpURLConnection connection = (HttpURLConnection) artifactUri.toURL().openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
boolean artifactExists = connection.getResponseCode() != 404;

With following dependency: org.eclipse.aether:aether-util:0.9.0.M2 and following imports:

import java.net.HttpURLConnection;
import java.net.URI;

import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.repository.layout.MavenDefaultLayout;
放我走吧 2024-10-21 14:15:50

由于被接受为正确的答案不再指向有效的 URL,并且因为我知道更好的方法,所以我发布了一个新答案。

wagon-maven-plugin,它有一个 存在目标。该文档有点不准确,但您可以使用它。

就代码而言,您可以查看 DefaultWagonDownload 类' 存在方法:

/**
 * 
 * @param wagon - a Wagon instance
 * @param resource - Remote resource to check
 * @throws WagonException
 */
public boolean exists( Wagon wagon, String resource )
    throws WagonException
{
    return wagon.resourceExists( resource );
}

Since the answer which is accepted as correct, is no longer pointing to valid URL-s and because I know a better way, I am posting a new answer.

There's the wagon-maven-plugin, which has an exist goal. The documentation is a bit inaccurate, but you can use that.

Code-wise, you can have a look at the DefaultWagonDownload class' exists method:

/**
 * 
 * @param wagon - a Wagon instance
 * @param resource - Remote resource to check
 * @throws WagonException
 */
public boolean exists( Wagon wagon, String resource )
    throws WagonException
{
    return wagon.resourceExists( resource );
}
甜味拾荒者 2024-10-21 14:15:50

如果您希望您的工件存在于远程 Maven 存储库中,我建议您只需使用 复制 maven-dependency-plugin 的魔力。

它将使用正常的 Maven 解析机制来检索工件,因此不会下载本地存储库中已有的内容。

在插件中,当使用maven 2(不确定maven3)时,您可以使用 mojo 执行器 来轻松从代码中调用魔力。

If you expect your artifacts being present in a remote maven repository, I'd suggest you simply use the copy mojo of the maven-dependency-plugin.

It will use normal maven resolution mechanism for retrieving artifacts so will not download something that is already in the local repository.

In a plugin, when using maven 2 (not sure about maven3) you can use the mojo executor to call a mojo from within you code easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文