Skip to content

Commit

Permalink
[FIXED JENKINS-10530] regression: plugins were no longer identified a…
Browse files Browse the repository at this point in the history
…s upstream snapshot dependencies
  • Loading branch information
kutzi committed Jul 30, 2011
1 parent eea67c7 commit fb9a266
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 31 deletions.
11 changes: 9 additions & 2 deletions core/src/main/java/hudson/model/DependencyGraph.java
Expand Up @@ -66,7 +66,7 @@
* @see Jenkins#getDependencyGraph()
* @author Kohsuke Kawaguchi
*/
public final class DependencyGraph implements Comparator<AbstractProject> {
public class DependencyGraph implements Comparator<AbstractProject> {

private Map<AbstractProject, List<DependencyGroup>> forward = new HashMap<AbstractProject, List<DependencyGroup>>();
private Map<AbstractProject, List<DependencyGroup>> backward = new HashMap<AbstractProject, List<DependencyGroup>>();
Expand All @@ -79,6 +79,9 @@ public final class DependencyGraph implements Comparator<AbstractProject> {
* Builds the dependency graph.
*/
public DependencyGraph() {
}

public void build() {
// Set full privileges while computing to avoid missing any projects the current user cannot see.
// Use setContext (NOT getContext().setAuthentication()) so we don't affect concurrent threads for same HttpSession.
SecurityContext saveCtx = SecurityContextHolder.getContext();
Expand All @@ -87,7 +90,7 @@ public DependencyGraph() {
NotSerilizableSecurityContext system = new NotSerilizableSecurityContext();
system.setAuthentication(ACL.SYSTEM);
SecurityContextHolder.setContext(system);
for( AbstractProject p : Jenkins.getInstance().getAllItems(AbstractProject.class) )
for( AbstractProject p : getAllProjects() )
p.buildDependencyGraph(this);

forward = finalize(forward);
Expand All @@ -99,6 +102,10 @@ public DependencyGraph() {
SecurityContextHolder.setContext(saveCtx);
}
}

Collection<AbstractProject> getAllProjects() {
return Jenkins.getInstance().getAllItems(AbstractProject.class);
}

/**
* Special constructor for creating an empty graph
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -3297,7 +3297,11 @@ public static boolean isCheckURIEncodingEnabled() {
* Rebuilds the dependency map.
*/
public void rebuildDependencyGraph() {
dependencyGraph = new DependencyGraph();
DependencyGraph graph = new DependencyGraph();
graph.build();
// volatile acts a as a memory barrier here and therefore guarantees
// that graph is fully build, before it's visible to other threads
dependencyGraph = graph;
}

public DependencyGraph getDependencyGraph() {
Expand Down
18 changes: 18 additions & 0 deletions maven-plugin/pom.xml
Expand Up @@ -332,6 +332,24 @@ THE SOFTWARE.
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.4.9</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
63 changes: 46 additions & 17 deletions maven-plugin/src/main/java/hudson/maven/MavenModule.java
Expand Up @@ -24,42 +24,55 @@
package hudson.maven;

import hudson.CopyOnWrite;
import hudson.Util;
import hudson.Functions;
import hudson.Util;
import hudson.maven.reporters.MavenMailer;
import hudson.model.*;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.DependencyGraph;
import hudson.model.Descriptor;
import hudson.model.Descriptor.FormException;
import jenkins.model.Jenkins;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.JDK;
import hudson.model.Job;
import hudson.model.Label;
import hudson.model.Node;
import hudson.model.Resource;
import hudson.model.Saveable;
import hudson.tasks.LogRotator;
import hudson.tasks.Publisher;
import hudson.tasks.Maven.MavenInstallation;
import hudson.tasks.Publisher;
import hudson.util.AlternativeUiTextProvider;
import hudson.util.DescribableList;
import org.apache.maven.project.MavenProject;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;

import com.google.common.util.concurrent.MoreExecutors;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;

import jenkins.model.Jenkins;

import org.apache.maven.project.MavenProject;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.export.Exported;

/**
* {@link Job} that builds projects based on Maven2.
*
* @author Kohsuke Kawaguchi
*/
public final class MavenModule extends AbstractMavenProject<MavenModule,MavenBuild> implements Saveable {
public class MavenModule extends AbstractMavenProject<MavenModule,MavenBuild> implements Saveable {
private DescribableList<MavenReporter,Descriptor<MavenReporter>> reporters =
new DescribableList<MavenReporter,Descriptor<MavenReporter>>(this);

Expand All @@ -79,6 +92,13 @@ public final class MavenModule extends AbstractMavenProject<MavenModule,MavenBui
* @since 1.199
*/
private String version;

/**
* Packaging type of the module.
*
* pom, jar, maven-plugin, ejb, war, ear, rar, par or other custom types.
*/
private String packaging;

private transient ModuleName moduleName;

Expand Down Expand Up @@ -175,6 +195,7 @@ public List<MavenModule> getSubsidiaries() {
/*package*/ void reconfigure(PomInfo pom) {
this.displayName = pom.displayName;
this.version = pom.version;
this.packaging = pom.packaging;
this.relativePath = pom.relativePath;
this.dependencies = pom.dependencies;
this.children = pom.children;
Expand Down Expand Up @@ -224,7 +245,7 @@ public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOExcep
if (d instanceof ModuleDependency) {
deps.add((ModuleDependency) d);
} else {
deps.add(new ModuleDependency((ModuleName)d, ModuleDependency.UNKNOWN));
deps.add(new ModuleDependency((ModuleName)d, ModuleDependency.UNKNOWN, false));
}
}
dependencies = deps;
Expand Down Expand Up @@ -304,7 +325,8 @@ public ModuleName getModuleName() {
* Gets groupId+artifactId+version as {@link ModuleDependency}.
*/
public ModuleDependency asDependency() {
return new ModuleDependency(moduleName,Functions.defaulted(version,ModuleDependency.UNKNOWN));
return new ModuleDependency(moduleName,Functions.defaulted(version,ModuleDependency.UNKNOWN),
PomInfo.PACKAGING_TYPE_PLUGIN.equals(this.packaging));
}

@Override
Expand Down Expand Up @@ -403,7 +425,7 @@ protected void buildDependencyGraph(DependencyGraph graph) {
if (data == null) {
Map<ModuleDependency,MavenModule> modules = new HashMap<ModuleDependency,MavenModule>();

for (MavenModule m : Jenkins.getInstance().getAllItems(MavenModule.class)) {
for (MavenModule m : getAllMavenModules()) {
if(!m.isBuildable()) continue;
ModuleDependency moduleDependency = m.asDependency();
MavenModule old = modules.get(moduleDependency);
Expand All @@ -419,7 +441,7 @@ protected void buildDependencyGraph(DependencyGraph graph) {
} else {
if (hasDependenciesWithUnknownVersion && !data.withUnknownVersions) {
// found 'old' MavenModule: add dependencies with unknown versions now
for (MavenModule m : Jenkins.getInstance().getAllItems(MavenModule.class)) {
for (MavenModule m : getAllMavenModules()) {
if(m.isDisabled()) continue;
ModuleDependency moduleDependency = m.asDependency().withUnknownVersion();
data.allModules.put(moduleDependency,m);
Expand Down Expand Up @@ -469,6 +491,13 @@ protected void buildDependencyGraph(DependencyGraph graph) {
}
}

/**
* Returns all Maven modules in this Jenkins instance.
*/
protected Collection<MavenModule> getAllMavenModules() {
return Jenkins.getInstance().getAllItems(MavenModule.class);
}

/**
* Check if this module has dependencies recorded without a concrete version -
* which shouldn't happen for any module which was at least build once with Jenkins >= 1.207.
Expand Down
Expand Up @@ -103,7 +103,7 @@
*
* @author Kohsuke Kawaguchi
*/
public final class MavenModuleSet extends AbstractMavenProject<MavenModuleSet,MavenModuleSetBuild> implements TopLevelItem, ItemGroup<MavenModule>, SCMedItem, Saveable, BuildableItemWithBuildWrappers {
public class MavenModuleSet extends AbstractMavenProject<MavenModuleSet,MavenModuleSetBuild> implements TopLevelItem, ItemGroup<MavenModule>, SCMedItem, Saveable, BuildableItemWithBuildWrappers {
/**
* All {@link MavenModule}s, keyed by their {@link MavenModule#getModuleName()} module name}s.
*/
Expand Down
26 changes: 17 additions & 9 deletions maven-plugin/src/main/java/hudson/maven/ModuleDependency.java
Expand Up @@ -46,19 +46,28 @@ public final class ModuleDependency implements Serializable {
/**
* @since 1.395
*/
public boolean plugin = false;
public final boolean plugin;

public ModuleDependency(String groupId, String artifactId, String version) {
this(groupId, artifactId, version, false);
}

public ModuleDependency(String groupId, String artifactId, String version, boolean plugin) {
this.groupId = groupId.intern();
this.artifactId = artifactId.intern();
if(version==null)
this.version = UNKNOWN;
else
this.version = version.intern();
this.plugin = plugin;
}

public ModuleDependency(ModuleName name, String version) {
this(name.groupId,name.artifactId,version);
this(name.groupId,name.artifactId,version,false);
}

public ModuleDependency(ModuleName name, String version, boolean plugin) {
this(name.groupId,name.artifactId,version,plugin);
}

public ModuleDependency(org.apache.maven.model.Dependency dep) {
Expand All @@ -70,26 +79,25 @@ public ModuleDependency(MavenProject project) {
}

public ModuleDependency(Plugin p) {
this(p.getGroupId(),p.getArtifactId(), Functions.defaulted(p.getVersion(),NONE));
this.plugin = true;
this(p.getGroupId(),p.getArtifactId(), Functions.defaulted(p.getVersion(),NONE),true);
}

public ModuleDependency(ReportPlugin p) {
this(p.getGroupId(),p.getArtifactId(),p.getVersion());
this.plugin = true;
this(p.getGroupId(),p.getArtifactId(),p.getVersion(),true);
}

public ModuleDependency(Extension ext) {
this(ext.getGroupId(),ext.getArtifactId(),ext.getVersion());
}

private ModuleDependency(String groupId, String artifactId) {
private ModuleDependency(String groupId, String artifactId, boolean plugin) {
// to be used only by the withUnknownVersion() method
// where we know that groupId and artifactId are already interned
// and where we want an UNKNOWN version
this.groupId = groupId;
this.artifactId = artifactId;
this.version = UNKNOWN;
this.plugin = plugin;
}

public ModuleName getName() {
Expand All @@ -103,7 +111,7 @@ public ModuleDependency withUnknownVersion() {
if (UNKNOWN.equals(version))
return this;
else
return new ModuleDependency(groupId,artifactId);
return new ModuleDependency(groupId,artifactId,plugin);
}

public boolean equals(Object o) {
Expand Down Expand Up @@ -131,7 +139,7 @@ public int hashCode() {
* Upon reading from the disk, intern strings.
*/
public ModuleDependency readResolve() {
return new ModuleDependency(groupId,artifactId,version);
return new ModuleDependency(groupId,artifactId,version,plugin);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion maven-plugin/src/main/java/hudson/maven/PomInfo.java
Expand Up @@ -48,6 +48,8 @@
* @author Kohsuke Kawaguchi
*/
final class PomInfo implements Serializable {

public static final String PACKAGING_TYPE_PLUGIN = "maven-plugin";

public final ModuleName name;

Expand Down Expand Up @@ -107,6 +109,8 @@ final class PomInfo implements Serializable {
private final String artifactId;

public final Notifier mailNotifier;

public final String packaging;

public PomInfo(MavenProject project, PomInfo parent, String relPath) {
this.name = new ModuleName(project);
Expand Down Expand Up @@ -154,13 +158,14 @@ public PomInfo(MavenProject project, PomInfo parent, String relPath) {

this.groupId = project.getGroupId();
this.artifactId = project.getArtifactId();
this.packaging = project.getPackaging();
}

/**
* Creates {@link ModuleDependency} that represents this {@link PomInfo}.
*/
private ModuleDependency asDependency() {
return new ModuleDependency(name,version);
return new ModuleDependency(name,version,PACKAGING_TYPE_PLUGIN.equals(this.packaging));
}

private void addPluginsAsDependencies(List<Plugin> plugins, Set<ModuleDependency> dependencies) {
Expand Down

0 comments on commit fb9a266

Please sign in to comment.