Search in sources :

Example 76 with ArtifactRepository

use of org.apache.maven.artifact.repository.ArtifactRepository in project fabric8 by jboss-fuse.

the class DeployToProfileMojo method uploadDeploymentUnit.

@SuppressWarnings("unchecked")
protected void uploadDeploymentUnit(J4pClient client, boolean customUsernameAndPassword) throws Exception {
    String uri = getMavenUploadUri(client);
    // lets resolve the artifact to make sure we get a local file
    Artifact artifact = project.getArtifact();
    ArtifactResolutionRequest request = new ArtifactResolutionRequest();
    request.setArtifact(artifact);
    addNeededRemoteRepository();
    request.setRemoteRepositories(remoteRepositories);
    request.setLocalRepository(localRepository);
    resolver.resolve(request);
    String packaging = project.getPackaging();
    File pomFile = project.getFile();
    @SuppressWarnings("unchecked") List<Artifact> attachedArtifacts = project.getAttachedArtifacts();
    DefaultRepositoryLayout layout = new DefaultRepositoryLayout();
    ArtifactRepository repo = new DefaultArtifactRepository(serverId, uri, layout);
    if (customUsernameAndPassword) {
        repo.setAuthentication(new Authentication(fabricServer.getUsername(), fabricServer.getPassword()));
    }
    // Deploy the POM
    boolean isPomArtifact = "pom".equals(packaging);
    if (!isPomArtifact) {
        ProjectArtifactMetadata metadata = new ProjectArtifactMetadata(artifact, pomFile);
        artifact.addMetadata(metadata);
    }
    try {
        if (isPomArtifact) {
            deploy(pomFile, artifact, repo, localRepository, retryFailedDeploymentCount);
        } else {
            File file = artifact.getFile();
            if (isFile(file)) {
                deploy(file, artifact, repo, localRepository, retryFailedDeploymentCount);
            } else if (!attachedArtifacts.isEmpty()) {
                getLog().info("No primary artifact to deploy, deploying attached artifacts instead.");
                Artifact pomArtifact = artifactFactory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion());
                pomArtifact.setFile(pomFile);
                deploy(pomFile, pomArtifact, repo, localRepository, retryFailedDeploymentCount);
                // propagate the timestamped version to the main artifact for the attached artifacts to pick it up
                artifact.setResolvedVersion(pomArtifact.getVersion());
            } else {
                String message = "The packaging for this project did not assign a file to the build artifact";
                throw new MojoExecutionException(message);
            }
        }
        for (Artifact attached : attachedArtifacts) {
            deploy(attached.getFile(), attached, repo, localRepository, retryFailedDeploymentCount);
        }
    } catch (ArtifactDeploymentException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
Also used : ProjectArtifactMetadata(org.apache.maven.project.artifact.ProjectArtifactMetadata) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactDeploymentException(org.apache.maven.artifact.deployer.ArtifactDeploymentException) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) DefaultRepositoryLayout(org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) Artifact(org.apache.maven.artifact.Artifact) ArtifactResolutionRequest(org.apache.maven.artifact.resolver.ArtifactResolutionRequest) Authentication(org.apache.maven.artifact.repository.Authentication) File(java.io.File)

Example 77 with ArtifactRepository

use of org.apache.maven.artifact.repository.ArtifactRepository in project liferay-ide by liferay.

the class NewMavenPluginProjectProvider method createNewProject.

@Override
public IStatus createNewProject(NewLiferayPluginProjectOp op, IProgressMonitor monitor) throws CoreException {
    ElementList<ProjectName> projectNames = op.getProjectNames();
    IStatus retval = null;
    IMavenConfiguration mavenConfiguration = MavenPlugin.getMavenConfiguration();
    IMavenProjectRegistry mavenProjectRegistry = MavenPlugin.getMavenProjectRegistry();
    IProjectConfigurationManager projectConfigurationManager = MavenPlugin.getProjectConfigurationManager();
    String groupId = op.getGroupId().content();
    String artifactId = op.getProjectName().content();
    String version = op.getArtifactVersion().content();
    String javaPackage = op.getGroupId().content();
    String activeProfilesValue = op.getActiveProfilesValue().content();
    IPortletFramework portletFramework = op.getPortletFramework().content(true);
    String frameworkName = NewLiferayPluginProjectOpMethods.getFrameworkName(op);
    IPath location = PathBridge.create(op.getLocation().content());
    if (location.lastSegment().equals(artifactId)) {
        // use parent dir since maven archetype will generate new dir under this
        // location
        location = location.removeLastSegments(1);
    }
    String archetypeArtifactId = op.getArchetype().content(true);
    Archetype archetype = new Archetype();
    String[] gav = archetypeArtifactId.split(":");
    String archetypeVersion = gav[gav.length - 1];
    archetype.setGroupId(gav[0]);
    archetype.setArtifactId(gav[1]);
    archetype.setVersion(archetypeVersion);
    ArchetypeManager archetypeManager = MavenPluginActivator.getDefault().getArchetypeManager();
    ArtifactRepository remoteArchetypeRepository = archetypeManager.getArchetypeRepository(archetype);
    Properties properties = new Properties();
    try {
        List<?> archProps = archetypeManager.getRequiredProperties(archetype, remoteArchetypeRepository, monitor);
        if (ListUtil.isNotEmpty(archProps)) {
            for (Object prop : archProps) {
                if (prop instanceof RequiredProperty) {
                    RequiredProperty rProp = (RequiredProperty) prop;
                    Value<PluginType> pluginType = op.getPluginType();
                    if (pluginType.content().equals(PluginType.theme)) {
                        String key = rProp.getKey();
                        if (key.equals("themeParent")) {
                            properties.put(key, op.getThemeParent().content(true));
                        } else if (key.equals("themeType")) {
                            properties.put(key, ThemeUtil.getTemplateExtension(op.getThemeFramework().content(true)));
                        }
                    } else {
                        properties.put(rProp.getKey(), rProp.getDefaultValue());
                    }
                }
            }
        }
    } catch (UnknownArchetype e1) {
        LiferayMavenCore.logError("Unable to find archetype required properties", e1);
    }
    ResolverConfiguration resolverConfig = new ResolverConfiguration();
    if (!CoreUtil.isNullOrEmpty(activeProfilesValue)) {
        resolverConfig.setSelectedProfiles(activeProfilesValue);
    }
    ProjectImportConfiguration configuration = new ProjectImportConfiguration(resolverConfig);
    List<IProject> newProjects = projectConfigurationManager.createArchetypeProjects(location, archetype, groupId, artifactId, version, javaPackage, properties, configuration, monitor);
    if (ListUtil.isNotEmpty(newProjects)) {
        op.setImportProjectStatus(true);
        for (IProject project : newProjects) {
            projectNames.insert().setName(project.getName());
        }
    }
    if (ListUtil.isEmpty(newProjects)) {
        retval = LiferayMavenCore.createErrorStatus("New project was not created due to unknown error");
    } else {
        IProject firstProject = newProjects.get(0);
        if (!CoreUtil.isNullOrEmpty(activeProfilesValue)) {
            String[] activeProfiles = activeProfilesValue.split(",");
            // find all profiles that should go in user settings file
            List<NewLiferayProfile> newUserSettingsProfiles = getNewProfilesToSave(activeProfiles, op.getNewLiferayProfiles(), ProfileLocation.userSettings);
            if (ListUtil.isNotEmpty(newUserSettingsProfiles)) {
                String userSettingsFile = mavenConfiguration.getUserSettingsFile();
                String userSettingsPath = null;
                if (CoreUtil.isNullOrEmpty(userSettingsFile)) {
                    userSettingsPath = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath();
                } else {
                    userSettingsPath = userSettingsFile;
                }
                try {
                    // backup user's settings.xml file
                    File settingsXmlFile = new File(userSettingsPath);
                    File backupFile = _getBackupFile(settingsXmlFile);
                    FileUtils.copyFile(settingsXmlFile, backupFile);
                    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                    Document pomDocument = docBuilder.parse(settingsXmlFile.getCanonicalPath());
                    for (NewLiferayProfile newProfile : newUserSettingsProfiles) {
                        MavenUtil.createNewLiferayProfileNode(pomDocument, newProfile);
                    }
                    TransformerFactory transformerFactory = TransformerFactory.newInstance();
                    Transformer transformer = transformerFactory.newTransformer();
                    DOMSource source = new DOMSource(pomDocument);
                    StreamResult result = new StreamResult(settingsXmlFile);
                    transformer.transform(source, result);
                } catch (Exception e) {
                    LiferayMavenCore.logError("Unable to save new Liferay profile to user settings.xml.", e);
                }
            }
            // find all profiles that should go in the project pom
            List<NewLiferayProfile> newProjectPomProfiles = getNewProfilesToSave(activeProfiles, op.getNewLiferayProfiles(), ProfileLocation.projectPom);
            // only need to set the first project as nested projects should pickup the
            // parent setting
            IMavenProjectFacade newMavenProject = mavenProjectRegistry.getProject(firstProject);
            IFile pomFile = newMavenProject.getPom();
            IDOMModel domModel = null;
            try {
                domModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(pomFile);
                for (NewLiferayProfile newProfile : newProjectPomProfiles) {
                    MavenUtil.createNewLiferayProfileNode(domModel.getDocument(), newProfile);
                }
                domModel.save();
            } catch (IOException ioe) {
                LiferayMavenCore.logError("Unable to save new Liferay profiles to project pom.", ioe);
            } finally {
                if (domModel != null) {
                    domModel.releaseFromEdit();
                }
            }
            for (IProject project : newProjects) {
                try {
                    projectConfigurationManager.updateProjectConfiguration(new MavenUpdateRequest(project, mavenConfiguration.isOffline(), true), monitor);
                } catch (Exception e) {
                    LiferayMavenCore.logError("Unable to update configuration for " + project.getName(), e);
                }
            }
            String pluginVersion = getNewLiferayProfilesPluginVersion(activeProfiles, op.getNewLiferayProfiles(), archetypeVersion);
            String archVersion = MavenUtil.getMajorMinorVersionOnly(archetypeVersion);
            updateDtdVersion(firstProject, pluginVersion, archVersion);
        }
        Value<PluginType> pluginType = op.getPluginType();
        if (pluginType.content().equals(PluginType.portlet)) {
            String portletName = op.getPortletName().content(false);
            retval = portletFramework.postProjectCreated(firstProject, frameworkName, portletName, monitor);
        }
    }
    if (retval == null) {
        retval = Status.OK_STATUS;
    }
    return retval;
}
Also used : ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) MavenUpdateRequest(org.eclipse.m2e.core.project.MavenUpdateRequest) IStatus(org.eclipse.core.runtime.IStatus) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) IFile(org.eclipse.core.resources.IFile) ProjectName(com.liferay.ide.project.core.model.ProjectName) Archetype(org.apache.maven.archetype.catalog.Archetype) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NewLiferayProfile(com.liferay.ide.project.core.model.NewLiferayProfile) IProjectConfigurationManager(org.eclipse.m2e.core.project.IProjectConfigurationManager) IMavenProjectRegistry(org.eclipse.m2e.core.project.IMavenProjectRegistry) Properties(java.util.Properties) Document(org.w3c.dom.Document) IMavenConfiguration(org.eclipse.m2e.core.embedder.IMavenConfiguration) RequiredProperty(org.apache.maven.archetype.metadata.RequiredProperty) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) TransformerFactory(javax.xml.transform.TransformerFactory) IPath(org.eclipse.core.runtime.IPath) StreamResult(javax.xml.transform.stream.StreamResult) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) IOException(java.io.IOException) PluginType(com.liferay.ide.project.core.model.PluginType) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) ArchetypeManager(org.eclipse.m2e.core.internal.archetype.ArchetypeManager) IPortletFramework(com.liferay.ide.project.core.IPortletFramework) ProjectImportConfiguration(org.eclipse.m2e.core.project.ProjectImportConfiguration) DocumentBuilder(javax.xml.parsers.DocumentBuilder) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 78 with ArtifactRepository

use of org.apache.maven.artifact.repository.ArtifactRepository in project tomee by apache.

the class TomEEMavenPluginRule method defaults.

protected static <T extends AbstractTomEEMojo> T defaults(final T tomEEMojo) {
    // settings
    final File settingsXml = new File(System.getProperty("user.home") + "/.m2/settings.xml");
    if (settingsXml.exists()) {
        try {
            final FileReader reader = new FileReader(settingsXml);
            try {
                tomEEMojo.settings = new SettingsXpp3Reader().read(reader, false);
            } finally {
                reader.close();
            }
        } catch (final Exception e) {
        // no-op
        }
    }
    tomEEMojo.project = new MavenProject() {

        @Override
        public Set getArtifacts() {
            return Collections.emptySet();
        }
    };
    if (tomEEMojo.settings == null) {
        tomEEMojo.settings = new Settings();
    }
    tomEEMojo.settings.setOffline(true);
    if (tomEEMojo.settings.getLocalRepository() == null || "".equals(tomEEMojo.settings.getLocalRepository())) {
        tomEEMojo.settings.setLocalRepository(System.getProperty("openejb.m2.home", System.getProperty("user.home") + "/.m2/repository"));
    }
    // we don't deploy anything by default
    tomEEMojo.skipCurrentProject = true;
    // our well known web profile ;)
    tomEEMojo.tomeeGroupId = "org.apache.tomee";
    tomEEMojo.tomeeArtifactId = "apache-tomee";
    tomEEMojo.tomeeVersion = OpenEjbVersion.get().getVersion();
    tomEEMojo.tomeeClassifier = "webprofile";
    tomEEMojo.tomeeType = "zip";
    // target config
    tomEEMojo.catalinaBase = new File("target/mvn-test");
    Files.mkdirs(tomEEMojo.catalinaBase);
    // some defaults
    tomEEMojo.simpleLog = true;
    tomEEMojo.quickSession = true;
    tomEEMojo.libDir = "lib";
    tomEEMojo.webappDir = "webapps";
    tomEEMojo.appDir = "apps";
    tomEEMojo.bin = new File(tomEEMojo.catalinaBase.getPath() + "-bin");
    tomEEMojo.config = new File(tomEEMojo.catalinaBase.getPath() + "-conf");
    tomEEMojo.lib = new File(tomEEMojo.catalinaBase.getPath() + "-lib");
    tomEEMojo.tomeeHttpPort = Integer.toString(NetworkUtil.getNextAvailablePort());
    tomEEMojo.tomeeAjpPort = Integer.toString(NetworkUtil.getNextAvailablePort());
    tomEEMojo.tomeeShutdownPort = Integer.toString(NetworkUtil.getNextAvailablePort());
    tomEEMojo.tomeeShutdownCommand = "SHUTDOWN";
    tomEEMojo.tomeeHost = "localhost";
    tomEEMojo.useConsole = true;
    tomEEMojo.checkStarted = true;
    tomEEMojo.checkStartedAttempts = 60;
    tomEEMojo.overrideOnUnzip = true;
    tomEEMojo.skipRootFolderOnUnzip = true;
    // we mock all the artifact resolution in test
    tomEEMojo.remoteRepos = new LinkedList<ArtifactRepository>();
    tomEEMojo.local = new DefaultArtifactRepository("local", tomEEMojo.settings.getLocalRepository(), new DefaultRepositoryLayout());
    tomEEMojo.factory = ArtifactFactory.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { ArtifactFactory.class }, new InvocationHandler() {

        @Override
        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
            return new DefaultArtifact(String.class.cast(args[0]), String.class.cast(args[1]), VersionRange.class.cast(args[2]), String.class.cast(args[5]), String.class.cast(args[3]), args[4] == null ? "" : String.class.cast(args[4]), null) {

                @Override
                public File getFile() {
                    return new File(tomEEMojo.settings.getLocalRepository(), getGroupId().replace('.', '/') + '/' + getArtifactId().replace('.', '/') + '/' + getVersion() + '/' + getArtifactId().replace('.', '/') + '-' + getVersion() + (args[4] == null ? "" : '-' + getClassifier()) + '.' + getType());
                }
            };
        }
    }));
    tomEEMojo.resolver = ArtifactResolver.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { ArtifactResolver.class }, new InvocationHandler() {

        @Override
        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
            return null;
        }
    }));
    return tomEEMojo;
}
Also used : Set(java.util.Set) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) DefaultRepositoryLayout(org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout) SettingsXpp3Reader(org.apache.maven.settings.io.xpp3.SettingsXpp3Reader) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) InvocationHandler(java.lang.reflect.InvocationHandler) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) ArtifactFactory(org.apache.maven.artifact.factory.ArtifactFactory) MavenProject(org.apache.maven.project.MavenProject) FileReader(java.io.FileReader) File(java.io.File) Settings(org.apache.maven.settings.Settings) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 79 with ArtifactRepository

use of org.apache.maven.artifact.repository.ArtifactRepository in project docker-maven-plugin by fabric8io.

the class MappingTrackArchiver method getLocalMavenRepoFile.

private File getLocalMavenRepoFile(MavenSession session, File source) {
    ArtifactRepository localRepo = session.getLocalRepository();
    if (localRepo == null) {
        log.warn("No local repo found so not adding any extra watches in the local repository");
        return null;
    }
    Artifact artifact = getArtifactFromJar(source);
    if (artifact != null) {
        try {
            return new File(localRepo.getBasedir(), localRepo.pathOf(artifact));
        } catch (InvalidArtifactRTException e) {
            log.warn("Cannot get the local repository path for %s in base dir %s : %s", artifact, localRepo.getBasedir(), e.getMessage());
        }
    }
    return null;
}
Also used : ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository)

Example 80 with ArtifactRepository

use of org.apache.maven.artifact.repository.ArtifactRepository in project tycho by eclipse.

the class TychoMirrorSelectorTest method testWithMatchingMirrorOfIds.

@Test
public void testWithMatchingMirrorOfIds() {
    ArtifactRepository repository = createArtifactRepository("neon-repo", "http://download.eclipse.org/eclipse/update/4.6");
    Mirror mirrorWithMatchingMirrorOfIds = createMirror("myId", "http://foo.bar", "neon-repo");
    Mirror selectedMirror = selector.getMirror(repository, Arrays.asList(mirrorWithMatchingMirrorOfIds));
    Assert.assertEquals(mirrorWithMatchingMirrorOfIds, selectedMirror);
}
Also used : ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) Mirror(org.apache.maven.settings.Mirror) Test(org.junit.Test)

Aggregations

ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)108 File (java.io.File)46 Artifact (org.apache.maven.artifact.Artifact)26 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)26 ArrayList (java.util.ArrayList)23 MavenProject (org.apache.maven.project.MavenProject)20 ArtifactRepositoryPolicy (org.apache.maven.artifact.repository.ArtifactRepositoryPolicy)17 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)15 IOException (java.io.IOException)13 DefaultRepositoryLayout (org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout)13 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)11 Properties (java.util.Properties)10 InvalidRepositoryException (org.apache.maven.artifact.InvalidRepositoryException)9 DefaultArtifactRepository (org.apache.maven.artifact.repository.DefaultArtifactRepository)9 Model (org.apache.maven.model.Model)9 DefaultArtifactHandler (org.apache.maven.artifact.handler.DefaultArtifactHandler)8 MojoFailureException (org.apache.maven.plugin.MojoFailureException)8 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)8 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)8 MavenProjectResourcesStub (org.apache.maven.plugin.resources.remote.stub.MavenProjectResourcesStub)7