Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preference to update snapshots for Maven projects #1217

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ public void update(IProject project, boolean force, IProgressMonitor monitor) th
Path pomPath = project.getFile("pom.xml").getLocation().toFile().toPath();
if (digestStore.updateDigest(pomPath) || force) {
JavaLanguageServerPlugin.logInfo("Starting Maven update for " + project.getName());
boolean updateSnapshots = JavaLanguageServerPlugin.getPreferencesManager() == null ? false : JavaLanguageServerPlugin.getPreferencesManager().getPreferences().isMavenUpdateSnapshots();
if (shouldCollectProjects()) {
Set<IProject> projectSet = new LinkedHashSet<>();
collectProjects(projectSet, project, monitor);
IProject[] projects = projectSet.toArray(new IProject[0]);
MavenUpdateRequest request = new MavenUpdateRequest(projects, MavenPlugin.getMavenConfiguration().isOffline(), true);
MavenUpdateRequest request = new MavenUpdateRequest(projects, MavenPlugin.getMavenConfiguration().isOffline(), updateSnapshots);
((ProjectConfigurationManager) configurationManager).updateProjectConfiguration(request, true, true, monitor);
} else {
MavenUpdateRequest request = new MavenUpdateRequest(project, MavenPlugin.getMavenConfiguration().isOffline(), true);
MavenUpdateRequest request = new MavenUpdateRequest(project, MavenPlugin.getMavenConfiguration().isOffline(), updateSnapshots);
configurationManager.updateProjectConfiguration(request, monitor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public class Preferences {
* Preference key to enable/disable downloading Maven source artifacts.
*/
public static final String MAVEN_DOWNLOAD_SOURCES = "java.maven.downloadSources";
/**
* Preference key to force update of Snapshots/Releases.
*/
public static final String MAVEN_UPDATE_SNAPSHOTS = "java.maven.updateSnapshots";
/**
* Preference key to enable/disable reference code lenses.
*/
Expand Down Expand Up @@ -316,6 +320,7 @@ public class Preferences {
private String gradleHome;
private boolean importMavenEnabled;
private boolean mavenDownloadSources;
private boolean mavenUpdateSnapshots;
private boolean implementationsCodeLensEnabled;
private boolean javaFormatEnabled;
private boolean javaFormatOnTypeEnabled;
Expand Down Expand Up @@ -433,6 +438,7 @@ public Preferences() {
gradleHome = null;
importMavenEnabled = true;
mavenDownloadSources = false;
mavenUpdateSnapshots = false;
referencesCodeLensEnabled = true;
implementationsCodeLensEnabled = false;
javaFormatEnabled = true;
Expand Down Expand Up @@ -499,6 +505,8 @@ public static Preferences createFrom(Map<String, Object> configuration) {
prefs.setImportMavenEnabled(importMavenEnabled);
boolean downloadSources = getBoolean(configuration, MAVEN_DOWNLOAD_SOURCES, false);
prefs.setMavenDownloadSources(downloadSources);
boolean updateSnapshots = getBoolean(configuration, MAVEN_UPDATE_SNAPSHOTS, false);
prefs.setMavenUpdateSnapshots(updateSnapshots);
boolean referenceCodelensEnabled = getBoolean(configuration, REFERENCES_CODE_LENS_ENABLED_KEY, true);
prefs.setReferencesCodelensEnabled(referenceCodelensEnabled);
boolean implementationCodeLensEnabled = getBoolean(configuration, IMPLEMENTATIONS_CODE_LENS_ENABLED_KEY, false);
Expand Down Expand Up @@ -692,6 +700,11 @@ public Preferences setMavenDownloadSources(boolean enabled) {
return this;
}

public Preferences setMavenUpdateSnapshots(boolean enabled) {
this.mavenUpdateSnapshots = enabled;
return this;
}

private Preferences setSignatureHelpEnabled(boolean enabled) {
this.signatureHelpEnabled = enabled;
return this;
Expand Down Expand Up @@ -895,6 +908,10 @@ public boolean isMavenDownloadSources() {
return mavenDownloadSources;
}

public boolean isMavenUpdateSnapshots() {
return mavenUpdateSnapshots;
}

public boolean isImplementationsCodeLensEnabled() {
return implementationsCodeLensEnabled;
}
Expand Down
20 changes: 20 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/maven/salut3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.bar</groupId>
<artifactId>salut3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies></dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.core.BinaryType;
import org.eclipse.jdt.ls.core.internal.DependencyUtil;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.JobHelpers;
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
import org.eclipse.jdt.ls.core.internal.SourceContentProvider;
import org.eclipse.jdt.ls.core.internal.WorkspaceHelper;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager.CHANGE_TYPE;
Expand Down Expand Up @@ -177,6 +179,42 @@ public void testDownloadSources() throws Exception {
assertNotNull(source);
}

@Test
public void testUpdateSnapshots() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test fail without the fix?

Copy link
Contributor Author

@snjeza snjeza Oct 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't fail. We are testing whether a dependency has actually been added when using the new preference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it doesn't fail with or without the fix, then the test proves nothing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new preference doesn't add any functionality. It just enhances performance. We are testing whether it breaks the update maven project action.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fbricon Do you want me to remove this test?

boolean updateSnapshots = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().isMavenUpdateSnapshots();
FeatureStatus status = preferenceManager.getPreferences().getUpdateBuildConfigurationStatus();
try {
IProject project = importMavenProject("salut3");
waitForBackgroundJobs();
IJavaProject javaProject = JavaCore.create(project);
IType type = javaProject.findType("org.apache.commons.lang3.StringUtils");
assertNull(type);
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setMavenUpdateSnapshots(false);
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setUpdateBuildConfigurationStatus(FeatureStatus.automatic);
IFile pom = project.getFile("pom.xml");
String content = ResourceUtils.getContent(pom);
//@formatter:off
content = content.replace("<dependencies></dependencies>",
"<dependencies>\n"
+ "<dependency>\n"
+ " <groupId>org.apache.commons</groupId>\n"
+ " <artifactId>commons-lang3</artifactId>\n"
+ " <version>3.9</version>\n"
+ "</dependency>"
+ "</dependencies>");
//@formatter:on
ResourceUtils.setContent(pom, content);
URI uri = pom.getRawLocationURI();
projectsManager.fileChanged(uri.toString(), CHANGE_TYPE.CHANGED);
waitForBackgroundJobs();
type = javaProject.findType("org.apache.commons.lang3.StringUtils");
assertNotNull(type);
} finally {
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setMavenUpdateSnapshots(updateSnapshots);
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setUpdateBuildConfigurationStatus(status);
}
}

@Test
public void testBatchImport() throws Exception {
IProject project = importMavenProject("batch");
Expand Down