*/
public IStatus configure(final IProgressMonitor monitor) {
final String userSettingsFileName = MavenPlugin.getMavenConfiguration().getUserSettingsFile();
this.oldUserSettingsFileName = userSettingsFileName;
UIJob job = new UIJob("Configure Maven Repository") {
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
try {
File globalRespository = MavenPlugin.getRepositoryRegistry().getLocalRepository()
.getBasedir();
if (ask(location.getCanonicalPath())) {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("file://" + location.getCanonicalPath()
+ File.separatorChar + "**");
for (Resource resource : resources) {
File fileResource = resource.getFile();
if (fileResource.isFile()) {
String relativePath = fileResource.getCanonicalPath().substring(
location.getCanonicalPath().length() + 1,
fileResource.getCanonicalPath().length());
monitor.subTask(String.format("Copying '%s'", relativePath));
File newFileResource = new File(globalRespository, relativePath);
if (!newFileResource.exists()) {
newFileResource.getParentFile().mkdirs();
FileCopyUtils.copy(fileResource, newFileResource);
}
}
}
}
else {
File userSettingsFile = new File(userSettingsFileName);
// check if the file already exists; if not create new one
if (!userSettingsFile.exists()) {
BufferedWriter writer = new BufferedWriter(new FileWriter(getSettingsFile()));
writer.append(SETTINGS_TEMPLATE);
writer.close();
}
else {
// if the settings.xml file exists; copy it into the workspace
FileCopyUtils.copy(userSettingsFile, getSettingsFile());
}
// load file to search for existing <localRepository> node
Document document = SpringCoreUtils.parseDocument(getSettingsFile().toURI());
NodeList nodes = document.getDocumentElement().getChildNodes();
boolean found = false;
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (LOCAL_REPOSITORY_ELEMENT_NAME.equals(node.getLocalName())
|| LOCAL_REPOSITORY_ELEMENT_NAME.equals(node.getNodeName())) {
// point to the detected maven repository
node.setTextContent(location.getCanonicalPath());
found = true;
break;
}
}
// if no <localRepository> node exists, create a new one
if (!found) {
Element node = document.createElement(LOCAL_REPOSITORY_ELEMENT_NAME);
node.appendChild(document.createTextNode(location.getCanonicalPath()));
document.getDocumentElement().appendChild(node);
}
// write the new settings file out to disk
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, document.getInputEncoding());
Writer out = new OutputStreamWriter(new FileOutputStream(getSettingsFile()),
document.getInputEncoding());
StreamResult result = new StreamResult(out);
DOMSource source = new DOMSource(document);
transformer.transform(source, result);
out.close();
// set the new settings.xml path
MavenPlugin.getMavenConfiguration()
.setUserSettingsFile(getSettingsFile().getCanonicalPath());
}
}
catch (Exception e) {
MavenCorePlugin.getDefault().getLog()
.log(new Status(IStatus.ERROR, MavenCorePlugin.PLUGIN_ID, 1, e.getMessage(), e));
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
};
// we need to wait until the UI job completes as otherwise the workbench might shut down
final boolean[] done = new boolean[1];
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
done[0] = true;
}
});
job.schedule();
while (!done[0]) {
try {
Thread.sleep(1000);
}