Revision: 19617 http://sourceforge.net/p/gate/code/19617 Author: markagreenwood Date: 2016-10-03 10:27:28 +0000 (Mon, 03 Oct 2016) Log Message: ----------- some more refactoring of the maven stuff and a folder for testing
Modified Paths: -------------- gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java Added Paths: ----------- gate/branches/sawdust2/gate-core/src/main/java/gate/util/maven/Utils.java gate/branches/sawdust2/gate-core/src/test/java/ gate/branches/sawdust2/gate-core/src/test/java/gate/ Modified: gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java =================================================================== --- gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java 2016-10-03 07:26:21 UTC (rev 19616) +++ gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java 2016-10-03 10:27:28 UTC (rev 19617) @@ -14,6 +14,10 @@ package gate; +import static gate.util.maven.Utils.getRepositorySession; +import static gate.util.maven.Utils.getRepositorySystem; +import static gate.util.maven.Utils.loadMavenSettings; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -51,36 +55,21 @@ import org.apache.maven.model.building.DefaultModelBuildingRequest; import org.apache.maven.model.building.ModelBuilder; import org.apache.maven.model.building.ModelBuildingRequest; -import org.apache.maven.repository.internal.MavenRepositorySystemUtils; import org.apache.maven.settings.Profile; import org.apache.maven.settings.Repository; import org.apache.maven.settings.Settings; -import org.apache.maven.settings.building.DefaultSettingsBuilder; -import org.apache.maven.settings.building.DefaultSettingsBuilderFactory; -import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; -import org.apache.maven.settings.building.SettingsBuildingException; -import org.apache.maven.settings.building.SettingsBuildingRequest; -import org.apache.maven.settings.building.SettingsBuildingResult; -import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.DefaultArtifact; import org.eclipse.aether.collection.CollectRequest; -import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory; import org.eclipse.aether.graph.Dependency; import org.eclipse.aether.graph.DependencyNode; -import org.eclipse.aether.impl.DefaultServiceLocator; -import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.ArtifactRequest; import org.eclipse.aether.resolution.ArtifactResult; import org.eclipse.aether.resolution.DependencyRequest; import org.eclipse.aether.resolution.DependencyResult; -import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; -import org.eclipse.aether.spi.connector.transport.TransporterFactory; -import org.eclipse.aether.transport.file.FileTransporterFactory; -import org.eclipse.aether.transport.http.HttpTransporterFactory; import org.eclipse.aether.util.artifact.SubArtifact; import org.jdom.Attribute; import org.jdom.Document; @@ -416,25 +405,6 @@ private static final long serialVersionUID = -6944695755723023537L; - public static final String userHome = System.getProperty("user.home"); - - public static final File userMavenConfigurationHome = - new File(userHome, ".m2"); - - public static final String envM2Home = System.getenv("M2_HOME"); - - public static final File DEFAULT_USER_SETTINGS_FILE = - new File(userMavenConfigurationHome, "settings.xml"); - - public static final String settingsXml = System.getProperty( - "M2_SETTINGS_XML", DEFAULT_USER_SETTINGS_FILE.getPath()); - - public static final File DEFAULT_GLOBAL_SETTINGS_FILE = - new File( - System.getProperty("maven.home", - envM2Home != null ? envM2Home : ""), - "conf/settings.xml"); - private String group, artifact, version; private String name; @@ -476,27 +446,6 @@ return true; } - protected static Settings loadMavenSettings() throws SettingsBuildingException { - // http://stackoverflow.com/questions/27818659/loading-mavens-settings-xml-for-jcabi-aether-to-use - SettingsBuildingRequest settingsBuildingRequest = - new DefaultSettingsBuildingRequest(); - settingsBuildingRequest.setSystemProperties(System.getProperties()); - settingsBuildingRequest.setUserSettingsFile(new File(settingsXml)); - settingsBuildingRequest - .setGlobalSettingsFile(DEFAULT_GLOBAL_SETTINGS_FILE); - - SettingsBuildingResult settingsBuildingResult; - DefaultSettingsBuilderFactory mvnSettingBuilderFactory = - new DefaultSettingsBuilderFactory(); - DefaultSettingsBuilder settingsBuilder = - mvnSettingBuilderFactory.newInstance(); - settingsBuildingResult = settingsBuilder.build(settingsBuildingRequest); - - Settings effectiveSettings = - settingsBuildingResult.getEffectiveSettings(); - return effectiveSettings; - } - @Override public void copyResources(File dir) throws IOException { @@ -573,9 +522,12 @@ artifactRequest.addRepository(remoteRepo); } } + + RepositorySystem repoSystem = getRepositorySystem(); + RepositorySystemSession repoSession = getRepositorySession(repoSystem); ArtifactResult artifactResult = - getRepositorySystem().resolveArtifact(getRepositorySession(), + repoSystem.resolveArtifact(repoSession, artifactRequest); baseURL = @@ -600,14 +552,14 @@ collectRequest.addRepository(central); //TODO don't we need repos from settings.xml in here as well? DependencyNode node = - getRepositorySystem().collectDependencies(getRepositorySession(), + repoSystem.collectDependencies(repoSession, collectRequest).getRoot(); DependencyRequest dependencyRequest = new DependencyRequest(); dependencyRequest.setRoot(node); DependencyResult result = - repoSystem.resolveDependencies(getRepositorySession(), + repoSystem.resolveDependencies(repoSession, dependencyRequest); // get the creole.xml out of the jar and add jar elements for this @@ -638,14 +590,14 @@ artifactRequest.setArtifact(artifactObj); artifactResult = - getRepositorySystem().resolveArtifact(getRepositorySession(), + repoSystem.resolveArtifact(repoSession, artifactRequest); ModelBuildingRequest req = new DefaultModelBuildingRequest(); req.setProcessPlugins(false); req.setPomFile(artifactResult.getArtifact().getFile()); - req.setModelResolver(new SimpleModelResolver(getRepositorySystem(), - getRepositorySession(), new ArrayList<RemoteRepository>())); + req.setModelResolver(new SimpleModelResolver(repoSystem, + repoSession, new ArrayList<RemoteRepository>())); req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); ModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance(); @@ -665,57 +617,7 @@ return jdomDoc; } - - - private static RepositorySystem repoSystem = null; - - private static DefaultRepositorySystemSession repoSystemSession = null; - - protected static RepositorySystem getRepositorySystem() { - if(repoSystem != null) return repoSystem; - - DefaultServiceLocator locator = - MavenRepositorySystemUtils.newServiceLocator(); - locator.addService(RepositoryConnectorFactory.class, - BasicRepositoryConnectorFactory.class); - locator.addService(TransporterFactory.class, FileTransporterFactory.class); - locator.addService(TransporterFactory.class, HttpTransporterFactory.class); - - repoSystem = locator.getService(RepositorySystem.class); - - return repoSystem; - } - - protected static RepositorySystemSession getRepositorySession() { - if(repoSystemSession != null) return repoSystemSession; - - repoSystemSession = MavenRepositorySystemUtils.newSession(); - - String repoLocation = System.getProperty("user.home") + File.separator - + ".m2" + File.separator + "repository/"; - try { - Settings effectiveSettings = loadMavenSettings(); - if(effectiveSettings.getLocalRepository() != null) { - repoLocation = effectiveSettings.getLocalRepository(); - } - } catch(Exception e) { - log.warn( - "Unable to load Maven settings, using default repository location", - e); - } - - LocalRepository localRepo = new LocalRepository(repoLocation); - log.info("Using local repository at: " + repoLocation); - repoSystemSession.setLocalRepositoryManager(getRepositorySystem() - .newLocalRepositoryManager(repoSystemSession, localRepo)); - - //repoSystemSession.setWorkspaceReader(new SimpleMavenCache(new File("repo"))); - - - return repoSystemSession; - } - @Override public String getName() { return name; @@ -826,10 +728,7 @@ public static void main(String args[]) throws Exception { Gate.runInSandbox(true); Gate.init(); - - - - + Plugin annieMaven = new Plugin.Maven("uk.ac.gate.plugins", "annie", "9.0-SNAPSHOT"); Gate.getCreoleRegister().registerPlugin(annieMaven); Added: gate/branches/sawdust2/gate-core/src/main/java/gate/util/maven/Utils.java =================================================================== --- gate/branches/sawdust2/gate-core/src/main/java/gate/util/maven/Utils.java (rev 0) +++ gate/branches/sawdust2/gate-core/src/main/java/gate/util/maven/Utils.java 2016-10-03 10:27:28 UTC (rev 19617) @@ -0,0 +1,107 @@ +package gate.util.maven; + +import java.io.File; + +import org.apache.log4j.Logger; +import org.apache.maven.repository.internal.MavenRepositorySystemUtils; +import org.apache.maven.settings.Settings; +import org.apache.maven.settings.building.DefaultSettingsBuilder; +import org.apache.maven.settings.building.DefaultSettingsBuilderFactory; +import org.apache.maven.settings.building.DefaultSettingsBuildingRequest; +import org.apache.maven.settings.building.SettingsBuildingException; +import org.apache.maven.settings.building.SettingsBuildingRequest; +import org.apache.maven.settings.building.SettingsBuildingResult; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory; +import org.eclipse.aether.impl.DefaultServiceLocator; +import org.eclipse.aether.repository.LocalRepository; +import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; +import org.eclipse.aether.spi.connector.transport.TransporterFactory; +import org.eclipse.aether.transport.file.FileTransporterFactory; +import org.eclipse.aether.transport.http.HttpTransporterFactory; + +public class Utils { + + private static final Logger log = Logger.getLogger(Utils.class); + + public static final String userHome = System.getProperty("user.home"); + + public static final File userMavenConfigurationHome = + new File(userHome, ".m2"); + + public static final String envM2Home = System.getenv("M2_HOME"); + + public static final File DEFAULT_USER_SETTINGS_FILE = + new File(userMavenConfigurationHome, "settings.xml"); + + public static final String settingsXml = System.getProperty( + "M2_SETTINGS_XML", DEFAULT_USER_SETTINGS_FILE.getPath()); + + public static final File DEFAULT_GLOBAL_SETTINGS_FILE = + new File( + System.getProperty("maven.home", + envM2Home != null ? envM2Home : ""), + "conf/settings.xml"); + + public static Settings loadMavenSettings() + throws SettingsBuildingException { + // http://stackoverflow.com/questions/27818659/loading-mavens-settings-xml-for-jcabi-aether-to-use + SettingsBuildingRequest settingsBuildingRequest = + new DefaultSettingsBuildingRequest(); + settingsBuildingRequest.setSystemProperties(System.getProperties()); + settingsBuildingRequest.setUserSettingsFile(new File(settingsXml)); + settingsBuildingRequest.setGlobalSettingsFile(DEFAULT_GLOBAL_SETTINGS_FILE); + + SettingsBuildingResult settingsBuildingResult; + DefaultSettingsBuilderFactory mvnSettingBuilderFactory = + new DefaultSettingsBuilderFactory(); + DefaultSettingsBuilder settingsBuilder = + mvnSettingBuilderFactory.newInstance(); + settingsBuildingResult = settingsBuilder.build(settingsBuildingRequest); + + Settings effectiveSettings = settingsBuildingResult.getEffectiveSettings(); + return effectiveSettings; + } + + public static RepositorySystem getRepositorySystem() { + + DefaultServiceLocator locator = + MavenRepositorySystemUtils.newServiceLocator(); + locator.addService(RepositoryConnectorFactory.class, + BasicRepositoryConnectorFactory.class); + locator.addService(TransporterFactory.class, FileTransporterFactory.class); + locator.addService(TransporterFactory.class, HttpTransporterFactory.class); + + return locator.getService(RepositorySystem.class); + } + + public static RepositorySystemSession getRepositorySession(RepositorySystem repoSystem) { + + DefaultRepositorySystemSession repoSystemSession = MavenRepositorySystemUtils.newSession(); + + String repoLocation = System.getProperty("user.home") + File.separator + + ".m2" + File.separator + "repository/"; + try { + Settings effectiveSettings = loadMavenSettings(); + if(effectiveSettings.getLocalRepository() != null) { + repoLocation = effectiveSettings.getLocalRepository(); + } + } catch(Exception e) { + log.warn( + "Unable to load Maven settings, using default repository location", + e); + } + + LocalRepository localRepo = new LocalRepository(repoLocation); + log.info("Using local repository at: " + repoLocation); + repoSystemSession.setLocalRepositoryManager(repoSystem + .newLocalRepositoryManager(repoSystemSession, localRepo)); + + //repoSystemSession.setWorkspaceReader(new SimpleMavenCache(new File("repo"))); + + + return repoSystemSession; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot _______________________________________________ GATE-cvs mailing list GATE-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gate-cvs