3
A
回答
4
與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 library做以下(基於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;
1
如果您希望您的工件出現在遠程Maven存儲庫中,我建議您只使用copy mojo of the maven-dependency-plugin
。
它將使用正常的maven解析機制來檢索工件,因此不會下載已存在於本地存儲庫中的東西。
在插件中,當使用maven 2(不確定maven3)時,您可以使用mojo executor輕鬆地從您的代碼中調用mojo。
1
由於被接受爲正確的答案不再指向有效的URL,因爲我知道更好的方法,所以我發佈了一個新答案。
還有wagon-maven-plugin,它有一個exist
的目標。文檔有點不準確,但你可以使用它。
代碼明智的,你可以看看DefaultWagonDownload類exists
方法:
/**
*
* @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);
}
相關問題
- 1. 如何檢查遠程存儲庫中是否存在Maven工件
- 2. 如何檢查文件是否存在
- 3. 如何檢查文件是否存在?
- 4. 如何檢查文件是否存在?
- 5. 如何檢查文件是否存在?
- 6. 如何檢查文件是否存在
- 7. 如何檢查工作表是否存在於Excel文件中
- 8. 如何檢查工作表是否存在於excel文件中
- 9. 檢查是否存在XML(不工作)
- 10. 檢查cookie是否存在不工作
- 11. 如何檢查命令是否存在?
- 12. 如何檢查是否MVC束存在
- 13. 如何檢查會話是否存在?
- 14. 如何檢查orderbychild值是否存在?
- 15. 如何檢查目錄是否存在?
- 16. 如何檢查函數是否存在?
- 17. 如何檢查cronjob是否存在PHP?
- 18. 如何檢查appSettings鍵是否存在?
- 19. 如何檢查ContactGroup是否存在
- 20. 如何檢查CakePHP是否存在Model?
- 21. 如何檢查對象是否存在?
- 22. 如何檢查數組是否存在?
- 23. 如何檢查記錄是否存在
- 24. 如何檢查CALayer是否存在
- 25. 如何檢查XML值是否存在?
- 26. 如何檢查視圖是否存在?
- 27. 如何檢查片段是否存在?
- 28. 如何檢查數組是否存在?
- 29. 如何檢查域是否存在
- 30. 如何檢查表是否存在
我要離開這個答案起來的情況下任何人想要查找的假象,而無需實際下載。 – Gili 2011-01-27 17:35:06
關於這個我不喜歡的部分是,事實上你實際上不僅僅在嘗試,而且實際上你正在下載這個工件。我懷疑這是你想要處理的大型文物。看看我的答案。 – carlspring 2013-07-05 16:21:13