我正在使用允許Fitnesse使用Maven存儲庫解析類路徑的Fitnesse Maven classpath plugin。Fitnesse Maven Classpath插件無法解析遠程依賴關係
該插件可以工作,但它似乎無法解決存儲在我的遠程存儲庫中的依賴關係。
它可以解決快照,如果它在我的本地存儲庫,但不是遠程。
如果我運行mvn install
(來自Fitnesse外部),那麼找到依賴關係時沒有問題,因此建議我的settings.xml
設置正確。
我調試了plugin,但我無法準確指出它爲什麼無法解析遠程快照。
問題:如何更改此設置以允許它解析快照?
編輯:添加更多詳細
這可以很容易地通過運行這個單元測試轉載:MavenClasspathExtractorTest
這個單元測試嘗試解決公地郎依賴。
如果您從本地存儲庫中刪除此依賴項,則測試將失敗,因爲它似乎無法從遠程存儲庫中檢索。
如果您將它放回本地存儲庫,它會再次通過。
這是問題的關鍵。
潛在的解決方案
下面是使用首先使用本地資源庫的jcabi-aether
庫潛在的解決方案,如果不會有從遠程存儲庫下載。這看起來不錯嗎?它適合我的需求
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Repository;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.*;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLoggerManager;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import com.jcabi.aether.Aether;
/**
* Utility class to extract classpath elements from Maven projects. Heavily based on code copied from Jenkin's Maven
* support.
*/
public class MavenClasspathExtractor {
public static final String userHome = System.getProperty("user.home");
public static final File userMavenConfigurationHome = new File(userHome, ".m2");
public static final String envM2Home = System.getenv("M2_HOME");
public final static String DEFAULT_SCOPE = "test";
public static final File DEFAULT_USER_SETTINGS_FILE = new File(userMavenConfigurationHome, "settings.xml");
public static final File DEFAULT_GLOBAL_SETTINGS_FILE =
new File(System.getProperty("maven.home", envM2Home != null ? envM2Home : ""), "conf/settings.xml");
private final Logger logger = new ConsoleLoggerManager().getLoggerForComponent("maven-classpath-plugin");
// Ensure M2_HOME variable is handled in a way similar to the mvn executable (script). To the extend possible.
static {
String m2Home = System.getenv().get("M2_HOME");
if (m2Home != null && System.getProperty("maven.home") == null) {
System.setProperty("maven.home", m2Home);
}
}
public List<String> extractClasspathEntries(File pomFile) throws MavenClasspathExtractionException {
return extractClasspathEntries(pomFile, DEFAULT_SCOPE);
}
public List<String> extractClasspathEntries(File pomFile, String scope) throws MavenClasspathExtractionException {
MavenXpp3Reader mavenReader;
FileReader reader = null;
try {
mavenReader = new MavenXpp3Reader();
reader = new FileReader(pomFile);
Model model = mavenReader.read(reader);
model.setPomFile(pomFile);
Collection<RemoteRepository> remoteRepositories = getRemoteRepositories();
File localRepo = new File(RepositorySystem.defaultUserLocalRepository.getAbsolutePath());
MavenProject project = new MavenProject(model);
Aether aether = new Aether(remoteRepositories, localRepo);
Artifact root = new DefaultArtifact(project.getGroupId(), project.getArtifactId(), project.getPackaging(), project.getVersion());
List<Artifact> artifacts = aether.resolve(root, scope);
List<String> paths = new ArrayList<>();
for(Artifact artifact : artifacts) {
paths.add(artifact.getFile().getAbsolutePath());
}
return paths;
} catch (Exception e) {
throw new MavenClasspathExtractionException(e);
} finally {
if(reader != null) {
try {
reader.close();
} catch (IOException e) {
throw new MavenClasspathExtractionException(e);
}
}
}
}
private Collection<RemoteRepository> getRemoteRepositories() throws SettingsBuildingException {
SettingsBuildingRequest settingsBuildingRequest = new DefaultSettingsBuildingRequest();
settingsBuildingRequest.setSystemProperties(System.getProperties());
settingsBuildingRequest.setUserSettingsFile(DEFAULT_USER_SETTINGS_FILE);
settingsBuildingRequest.setGlobalSettingsFile(DEFAULT_GLOBAL_SETTINGS_FILE);
DefaultSettingsBuilderFactory mvnSettingBuilderFactory = new DefaultSettingsBuilderFactory();
DefaultSettingsBuilder settingsBuilder = mvnSettingBuilderFactory.newInstance();
SettingsBuildingResult settingsBuildingResult = settingsBuilder.build(settingsBuildingRequest);
Settings effectiveSettings = settingsBuildingResult.getEffectiveSettings();
Map<String, Profile> profilesMap = effectiveSettings.getProfilesAsMap();
Collection<RemoteRepository> remotes = new ArrayList<RemoteRepository>(20);
for (String profileName : effectiveSettings.getActiveProfiles())
{
Profile profile = profilesMap.get(profileName);
List<Repository> repositories = profile.getRepositories();
for (Repository repo : repositories)
{
RemoteRepository remoteRepo
= new RemoteRepository(repo.getId(), "default", repo.getUrl());
remotes.add(remoteRepo);
}
}
return remotes;
}
}
您能否提供更多詳細信息?最重要的是,您實際使用的'pom.xml'文件(或相關/縮減的部分)? –
在上面添加更多細節 – DJ180