Search in sources :

Example 1 with IMaven

use of org.eclipse.m2e.core.embedder.IMaven in project eclipse.jdt.ls by eclipse.

the class DependencyUtil method getLocalArtifactFile.

// From org.eclipse.m2e.jdt.internal.BuildPathManager#getAttachedArtifactFile
private static File getLocalArtifactFile(ArtifactKey a) {
    // can't use Maven resolve methods since they mark artifacts as not-found even if they could be resolved remotely
    IMaven maven = MavenPlugin.getMaven();
    try {
        ArtifactRepository localRepository = maven.getLocalRepository();
        String relPath = // $NON-NLS-1$
        maven.getArtifactPath(// $NON-NLS-1$
        localRepository, // $NON-NLS-1$
        a.getGroupId(), // $NON-NLS-1$
        a.getArtifactId(), // $NON-NLS-1$
        a.getVersion(), // $NON-NLS-1$
        "jar", a.getClassifier());
        File file = new File(localRepository.getBasedir(), relPath).getCanonicalFile();
        if (file.canRead() && file.isFile()) {
            return file;
        }
    } catch (CoreException | IOException ex) {
    // fall through
    }
    return null;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) IOException(java.io.IOException) File(java.io.File) IMaven(org.eclipse.m2e.core.embedder.IMaven)

Example 2 with IMaven

use of org.eclipse.m2e.core.embedder.IMaven in project liferay-ide by liferay.

the class MavenModuleProjectTests method testNewLiferayComponentBndAndMavenForPortleActionCommandAndRest.

@Test
public void testNewLiferayComponentBndAndMavenForPortleActionCommandAndRest() throws Exception {
    NewLiferayModuleProjectOp pop = NewLiferayModuleProjectOp.TYPE.instantiate();
    pop.setProjectName("testMavenModuleComponentBnd");
    pop.setProjectTemplateName("portlet");
    pop.setProjectProvider("maven-module");
    Status modulePorjectStatus = NewLiferayModuleProjectOpMethods.execute(pop, ProgressMonitorBridge.create(new NullProgressMonitor()));
    assertTrue(modulePorjectStatus.ok());
    IProject modPorject = CoreUtil.getProject(pop.getProjectName().content());
    modPorject.open(new NullProgressMonitor());
    NewLiferayComponentOp cop = NewLiferayComponentOp.TYPE.instantiate();
    cop.setProjectName(pop.getProjectName().content());
    cop.setComponentClassTemplateName("PortletActionCommand");
    NewLiferayComponentOpMethods.execute(cop, ProgressMonitorBridge.create(new NullProgressMonitor()));
    IFile bgd = modPorject.getFile("bnd.bnd");
    String bndcontent = FileUtil.readContents(bgd.getLocation().toFile(), true);
    String bndConfig = "-includeresource: \\" + System.getProperty("line.separator") + "\t" + "@com.liferay.util.bridges-2.0.0.jar!/com/liferay/util/bridges/freemarker/FreeMarkerPortlet.class,\\" + System.getProperty("line.separator") + "\t" + "@com.liferay.util.taglib-2.0.0.jar!/META-INF/*.tld" + System.getProperty("line.separator");
    assertTrue(bndcontent.contains(bndConfig));
    IFile pomFile = modPorject.getFile(IMavenConstants.POM_FILE_NAME);
    final IMaven maven = MavenPlugin.getMaven();
    Model model = maven.readModel(pomFile.getLocation().toFile());
    List<Dependency> dependencies = model.getDependencies();
    boolean hasDependency = false;
    for (Dependency de : dependencies) {
        String managementKey = de.getManagementKey();
        if (managementKey.equals("com.liferay.portal:com.liferay.util.bridges:jar")) {
            hasDependency = true;
            break;
        }
    }
    assertTrue(hasDependency);
    NewLiferayComponentOp copRest = NewLiferayComponentOp.TYPE.instantiate();
    copRest.setProjectName(pop.getProjectName().content());
    copRest.setComponentClassTemplateName("RestService");
    NewLiferayComponentOpMethods.execute(copRest, ProgressMonitorBridge.create(new NullProgressMonitor()));
    bgd = modPorject.getFile("bnd.bnd");
    bndcontent = FileUtil.readContents(bgd.getLocation().toFile(), true);
    assertTrue(bndcontent.contains(bndConfig));
    final String restConfig = "Require-Capability: osgi.contract; filter:=\"(&(osgi.contract=JavaJAXRS)(version=2))\"";
    assertTrue(bndcontent.contains(restConfig));
    model = maven.readModel(pomFile.getLocation().toFile());
    dependencies = model.getDependencies();
    hasDependency = false;
    for (Dependency de : dependencies) {
        String managementKey = de.getManagementKey();
        if (managementKey.equals("javax.ws.rs:javax.ws.rs-api:jar")) {
            hasDependency = true;
            break;
        }
    }
    assertTrue(hasDependency);
    NewLiferayComponentOp copAuth = NewLiferayComponentOp.TYPE.instantiate();
    copAuth.setProjectName(pop.getProjectName().content());
    copAuth.setComponentClassTemplateName("Authenticator");
    NewLiferayComponentOpMethods.execute(copAuth, ProgressMonitorBridge.create(new NullProgressMonitor()));
    bgd = modPorject.getFile("bnd.bnd");
    bndcontent = FileUtil.readContents(bgd.getLocation().toFile(), true);
    bndConfig = "-includeresource: \\" + System.getProperty("line.separator") + "\t" + "@com.liferay.util.bridges-2.0.0.jar!/com/liferay/util/bridges/freemarker/FreeMarkerPortlet.class,\\" + System.getProperty("line.separator") + "\t" + "@com.liferay.util.taglib-2.0.0.jar!/META-INF/*.tld,\\" + System.getProperty("line.separator") + "\t" + "@shiro-core-1.1.0.jar";
    assertTrue(bndcontent.contains(bndConfig));
    model = maven.readModel(pomFile.getLocation().toFile());
    dependencies = model.getDependencies();
    hasDependency = false;
    for (Dependency de : dependencies) {
        String managementKey = de.getManagementKey();
        if (managementKey.equals("org.apache.shiro:shiro-core:jar")) {
            hasDependency = true;
            break;
        }
    }
    assertTrue(hasDependency);
}
Also used : Status(org.eclipse.sapphire.modeling.Status) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) NewLiferayModuleProjectOp(com.liferay.ide.project.core.modules.NewLiferayModuleProjectOp) NewLiferayComponentOp(com.liferay.ide.project.core.modules.NewLiferayComponentOp) Model(org.apache.maven.model.Model) Dependency(org.apache.maven.model.Dependency) IProject(org.eclipse.core.resources.IProject) IMaven(org.eclipse.m2e.core.embedder.IMaven) Test(org.junit.Test)

Example 3 with IMaven

use of org.eclipse.m2e.core.embedder.IMaven in project liferay-ide by liferay.

the class MavenUtil method executeGoals.

public static IStatus executeGoals(IMavenProjectFacade facade, IMavenExecutionContext context, List<String> goals, IProgressMonitor monitor) throws CoreException {
    IMaven maven = MavenPlugin.getMaven();
    MavenProject mavenProject = facade.getMavenProject(monitor);
    MavenExecutionPlan plan = maven.calculateExecutionPlan(mavenProject, goals, true, monitor);
    List<MojoExecution> mojos = plan.getMojoExecutions();
    ResolverConfiguration configuration = facade.getResolverConfiguration();
    configuration.setResolveWorkspaceProjects(true);
    for (MojoExecution mojo : mojos) {
        maven.execute(mavenProject, mojo, monitor);
    }
    return Status.OK_STATUS;
}
Also used : ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) MavenExecutionPlan(org.apache.maven.lifecycle.MavenExecutionPlan) IMaven(org.eclipse.m2e.core.embedder.IMaven)

Example 4 with IMaven

use of org.eclipse.m2e.core.embedder.IMaven in project liferay-ide by liferay.

the class MavenUtil method getLocalRepositoryDir.

public static String getLocalRepositoryDir() {
    String retval = null;
    IMavenConfiguration mavenConfiguration = MavenPlugin.getMavenConfiguration();
    String userSettings = mavenConfiguration.getUserSettingsFile();
    if ((userSettings == null) || (userSettings.length() == 0)) {
        userSettings = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath();
    }
    String globalSettings = MavenPlugin.getMavenConfiguration().getGlobalSettingsFile();
    IMaven maven = MavenPlugin.getMaven();
    try {
        Settings settings = maven.buildSettings(globalSettings, userSettings);
        retval = settings.getLocalRepository();
    } catch (CoreException ce) {
        LiferayMavenCore.logError("Unable to get local repository dir.", ce);
    }
    if (retval == null) {
        retval = org.apache.maven.repository.RepositorySystem.defaultUserLocalRepository.getAbsolutePath();
    }
    return retval;
}
Also used : IMavenConfiguration(org.eclipse.m2e.core.embedder.IMavenConfiguration) CoreException(org.eclipse.core.runtime.CoreException) IMaven(org.eclipse.m2e.core.embedder.IMaven) Settings(org.apache.maven.settings.Settings)

Example 5 with IMaven

use of org.eclipse.m2e.core.embedder.IMaven in project liferay-ide by liferay.

the class NewMavenPluginProjectProvider method validateProjectLocation.

@Override
public IStatus validateProjectLocation(String projectName, IPath path) {
    IStatus retval = Status.OK_STATUS;
    /*
		 * if the path is a folder and it has a pom.xml that is a package type of 'pom'
		 * then this is a valid location if projectName is null or empty , don't need to check
		 * just return
		 */
    if (CoreUtil.isNullOrEmpty(projectName)) {
        return retval;
    }
    File dir = path.toFile();
    if (FileUtil.notExists(dir)) {
        return retval;
    }
    File pomFile = path.append(IMavenConstants.POM_FILE_NAME).toFile();
    if (FileUtil.exists(pomFile)) {
        IMaven maven = MavenPlugin.getMaven();
        try {
            Model result = maven.readModel(pomFile);
            if (!"pom".equals(result.getPackaging())) {
                retval = LiferayMavenCore.createErrorStatus("\"" + pomFile.getParent() + "\" contains a non-parent maven project.");
            } else {
                String name = result.getName();
                if (projectName.equals(name)) {
                    retval = LiferayMavenCore.createErrorStatus("The project name \"" + projectName + "\" can't be the same as the parent.");
                } else {
                    IPath newProjectPath = path.append(projectName);
                    retval = validateProjectLocation(projectName, newProjectPath);
                }
            }
        } catch (CoreException ce) {
            retval = LiferayMavenCore.createErrorStatus("Invalid project location.", ce);
            LiferayMavenCore.log(retval);
        }
    } else {
        File[] files = dir.listFiles();
        if (ListUtil.isNotEmpty(files)) {
            retval = LiferayMavenCore.createErrorStatus("Project location is not empty or a parent pom.");
        }
    }
    return retval;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) Model(org.apache.maven.model.Model) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IMaven(org.eclipse.m2e.core.embedder.IMaven)

Aggregations

IMaven (org.eclipse.m2e.core.embedder.IMaven)14 CoreException (org.eclipse.core.runtime.CoreException)9 MavenProject (org.apache.maven.project.MavenProject)7 File (java.io.File)6 IStatus (org.eclipse.core.runtime.IStatus)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 MojoExecution (org.apache.maven.plugin.MojoExecution)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)5 MavenExecutionPlan (org.apache.maven.lifecycle.MavenExecutionPlan)4 Model (org.apache.maven.model.Model)4 IFile (org.eclipse.core.resources.IFile)4 SubMonitor (org.eclipse.core.runtime.SubMonitor)3 IMavenExecutionContext (org.eclipse.m2e.core.embedder.IMavenExecutionContext)3 NewLiferayProfile (com.liferay.ide.project.core.model.NewLiferayProfile)2 IOException (java.io.IOException)2 Dependency (org.apache.maven.model.Dependency)2 Profile (org.apache.maven.settings.Profile)2 VersionRangeResolutionException (org.eclipse.aether.resolution.VersionRangeResolutionException)2 Version (org.eclipse.aether.version.Version)2