maven JPS: read 'repository' type library properties

This commit is contained in:
Eugene Zhuravlev
2016-06-14 14:03:35 +02:00
parent e630240e1d
commit 8725ca70a0
5 changed files with 170 additions and 33 deletions
@@ -111,7 +111,7 @@ public class JpsLibraryTableSerializer {
}
for (Element rootsElement : libraryElement.getChildren()) {
final String rootTypeId = rootsElement.getName();
if (!rootTypeId.equals(JAR_DIRECTORY_TAG)) {
if (!rootTypeId.equals(JAR_DIRECTORY_TAG) && !rootTypeId.equals(PROPERTIES_TAG)) {
final JpsOrderRootType rootType = getRootType(rootTypeId);
for (Element rootElement : JDOMUtil.getChildren(rootsElement, ROOT_TAG)) {
String url = rootElement.getAttributeValue(URL_ATTRIBUTE);
@@ -0,0 +1,84 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jps.maven.model;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Eugene Zhuravlev
* Date: 13-Jun-16
*/
public class RepositoryLibraryDescriptor {
private final String myMavenId;
private final String myGroupId;
private final String myArtifactId;
private final String myVersion;
public RepositoryLibraryDescriptor(@NotNull String groupId, @NotNull String artifactId, @NotNull String version) {
myGroupId = groupId;
myArtifactId = artifactId;
myVersion = version;
myMavenId = groupId + ":" + artifactId + ":" + version;
}
public RepositoryLibraryDescriptor(@Nullable String mavenId) {
myMavenId = mavenId;
if (mavenId == null) {
myGroupId = myArtifactId = myVersion = null;
}
else {
String[] parts = mavenId.split(":");
myGroupId = parts.length > 0 ? parts[0] : null;
myArtifactId = parts.length > 1 ? parts[1] : null;
myVersion = parts.length > 2 ? parts[2] : null;
}
}
public String getMavenId() {
return myMavenId;
}
public String getGroupId() {
return myGroupId;
}
public String getArtifactId() {
return myArtifactId;
}
public String getVersion() {
return myVersion;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RepositoryLibraryDescriptor that = (RepositoryLibraryDescriptor)o;
if (myMavenId != null ? !myMavenId.equals(that.myMavenId) : that.myMavenId != null) return false;
return true;
}
@Override
public int hashCode() {
return myMavenId != null ? myMavenId.hashCode() : 0;
}
}
@@ -6,6 +6,10 @@ import org.jetbrains.jps.maven.model.JpsMavenExtensionService;
import org.jetbrains.jps.model.module.JpsDependencyElement;
import org.jetbrains.jps.model.module.JpsModule;
import org.jetbrains.jps.model.serialization.JpsModelSerializerExtension;
import org.jetbrains.jps.model.serialization.library.JpsLibraryPropertiesSerializer;
import java.util.Collections;
import java.util.List;
/**
* @author nik
@@ -41,4 +45,10 @@ public class JpsMavenModelSerializationExtension extends JpsModelSerializerExten
orderEntry.setAttribute(PRODUCTION_ON_TEST_ATTRIBUTE, "");
}
}
@NotNull
@Override
public List<? extends JpsLibraryPropertiesSerializer<?>> getLibraryPropertiesSerializers() {
return Collections.singletonList(JpsMavenRepositoryLibraryType.createPropertiesSerializer());
}
}
@@ -0,0 +1,56 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jps.maven.model.impl;
import org.jdom.Element;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.maven.model.RepositoryLibraryDescriptor;
import org.jetbrains.jps.model.JpsElementFactory;
import org.jetbrains.jps.model.JpsSimpleElement;
import org.jetbrains.jps.model.ex.JpsElementTypeBase;
import org.jetbrains.jps.model.library.JpsLibraryType;
import org.jetbrains.jps.model.serialization.library.JpsLibraryPropertiesSerializer;
public class JpsMavenRepositoryLibraryType extends JpsElementTypeBase<JpsSimpleElement<RepositoryLibraryDescriptor>>
implements JpsLibraryType<JpsSimpleElement<RepositoryLibraryDescriptor>> {
public static JpsMavenRepositoryLibraryType INSTANCE = new JpsMavenRepositoryLibraryType();
private static final String MAVEN_ID_ATTRIBUTE = "maven-id";
/** @noinspection MethodMayBeStatic*/
public final String getTypeId() {
return "repository";
}
public static JpsLibraryPropertiesSerializer<JpsSimpleElement<RepositoryLibraryDescriptor>> createPropertiesSerializer() {
return new JpsLibraryPropertiesSerializer<JpsSimpleElement<RepositoryLibraryDescriptor>>(INSTANCE, INSTANCE.getTypeId()) {
@Override
public JpsSimpleElement<RepositoryLibraryDescriptor> loadProperties(@Nullable Element elem) {
return JpsElementFactory.getInstance().createSimpleElement(new RepositoryLibraryDescriptor(
elem != null? elem.getAttributeValue(MAVEN_ID_ATTRIBUTE, (String)null) : null
));
}
@Override
public void saveProperties(JpsSimpleElement<RepositoryLibraryDescriptor> properties, Element element) {
final String mavenId = properties.getData().getMavenId();
if (mavenId != null) {
element.setAttribute(MAVEN_ID_ATTRIBUTE, mavenId);
}
}
};
}
}
@@ -19,38 +19,30 @@ import com.intellij.openapi.roots.libraries.LibraryProperties;
import com.intellij.openapi.util.Comparing;
import com.intellij.util.xmlb.annotations.Attribute;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jps.maven.model.RepositoryLibraryDescriptor;
import java.util.function.Function;
/**
* @author nik
*/
public class RepositoryLibraryProperties extends LibraryProperties<RepositoryLibraryProperties> {
private String mavenId;
private String groupId;
private String artifactId;
private String version;
private RepositoryLibraryDescriptor myDescriptor;
public RepositoryLibraryProperties() {
}
public RepositoryLibraryProperties(String mavenId) {
setMavenId(mavenId);
myDescriptor = new RepositoryLibraryDescriptor(mavenId);
}
public RepositoryLibraryProperties(@NotNull String groupId, @NotNull String artifactId, @NotNull String version) {
this.groupId = groupId;
this.artifactId = artifactId;
this.version = version;
this.mavenId = groupId + ":" + artifactId + ":" + version;
myDescriptor = new RepositoryLibraryDescriptor(groupId, artifactId, version);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof RepositoryLibraryProperties)) {
return false;
}
RepositoryLibraryProperties other = (RepositoryLibraryProperties)obj;
return Comparing.equal(mavenId, other.mavenId);
return obj instanceof RepositoryLibraryProperties && Comparing.equal(myDescriptor, ((RepositoryLibraryProperties)obj).myDescriptor);
}
@Override
@@ -65,41 +57,36 @@ public class RepositoryLibraryProperties extends LibraryProperties<RepositoryLib
@Override
public void loadState(RepositoryLibraryProperties state) {
setMavenId(state.mavenId);
myDescriptor = state.myDescriptor;
}
@Attribute("maven-id")
public String getMavenId() {
return mavenId;
return call(RepositoryLibraryDescriptor::getMavenId);
}
public void setMavenId(String mavenId) {
this.mavenId = mavenId;
if (mavenId == null) {
groupId = artifactId = version = null;
}
else {
String[] parts = mavenId.split(":");
groupId = parts.length > 0 ? parts[0] : null;
artifactId = parts.length > 1 ? parts[1] : null;
version = parts.length > 2 ? parts[2] : null;
}
myDescriptor = new RepositoryLibraryDescriptor(mavenId);
}
public String getGroupId() {
return groupId;
return call(RepositoryLibraryDescriptor::getGroupId);
}
public String getArtifactId() {
return artifactId;
return call(RepositoryLibraryDescriptor::getArtifactId);
}
public String getVersion() {
return version;
return call(RepositoryLibraryDescriptor::getVersion);
}
public void changeVersion(String version) {
this.version = version;
this.mavenId = groupId + ":" + artifactId + ":" + version;
myDescriptor = new RepositoryLibraryDescriptor(getGroupId(), getArtifactId(), version);
}
private String call(Function<RepositoryLibraryDescriptor, String> method) {
final RepositoryLibraryDescriptor descriptor = myDescriptor;
return descriptor != null ? method.apply(descriptor) : null;
}
}