Thursday March 12, 2009
Downloading JARs from Maven into IntelliJ Projects
The com.intellij.facet.ui.libraries.MavenLibraryUtil.createMavenJarInfo class is quite interesting because it lets a framework support plugin download the related JARs directly from Maven.
Here's a simple utility class that gets two JARs from Maven's default repo, i.e., ibilio.org:
import com.intellij.facet.ui.libraries.LibraryInfo;
import static com.intellij.facet.ui.libraries.MavenLibraryUtil.createMavenJarInfo;
import org.jetbrains.annotations.NonNls;
public enum DemoVersion {
VisualVM("1", new LibraryInfo[]{
createMavenJarInfo("dbunit", "1.5.1", ""),
createMavenJarInfo("lucene", "1.4.3", ""),
}, null
);
private final String myName;
private final LibraryInfo[] myJars;
private final LibraryInfo myDummyLib;
private DemoVersion(@NonNls String name, LibraryInfo[] infos, LibraryInfo dummyTaglib) {
myName = name;
myJars = infos;
myDummyLib = dummyTaglib;
}
public LibraryInfo[] getJars() {
return myJars;
}
public LibraryInfo getMyDummyLib() {
return myDummyLib;
}
public String toString() {
return myName;
}
}
The code above is based on the Struts version of this class in IntelliJ.
Note the ENUM, with "1" as the version number, which maps to the "getVersions" method in the code below:
//Add a single version, just to have one for now:
@Override
@NotNull
public String[] getVersions() {
List<String> versions = new ArrayList<String>();
versions.add("1");
return versions.toArray(new String[versions.size()]);
}
@NotNull
@Override
protected LibraryInfo[] getLibraries(final String selectedVersion) {
DemoVersion version = getVersion(selectedVersion);
LibraryInfo[] jars = version.getJars();
LibraryInfo myTaglib = version.getMyDummyLib();
if (version == DemoVersion.VisualVM & myTaglib != null) {
jars = ArrayUtil.append(jars, myTaglib, LibraryInfo.class);
}
return jars;
}
private static DemoVersion getVersion(String versionName) {
for (DemoVersion version : DemoVersion.values()) {
if (versionName.equals(version.toString())) {
return version;
}
}
return null;
}
The above is in the FacetTypeFrameworkSupportProvider<DemoFacet> class.
What all of the above means is that once I've selected my framework, I have the two JARs downloaded from Maven and added to my project, ready to use as soon as the project is created.
Mar 12 2009, 12:11:12 PM PDT Permalink


