Example usage for org.apache.maven.artifact Artifact VERSION_FILE_PATTERN

List of usage examples for org.apache.maven.artifact Artifact VERSION_FILE_PATTERN

Introduction

In this page you can find the example usage for org.apache.maven.artifact Artifact VERSION_FILE_PATTERN.

Prototype

Pattern VERSION_FILE_PATTERN

To view the source code for org.apache.maven.artifact Artifact VERSION_FILE_PATTERN.

Click Source Link

Usage

From source file:io.sarl.m2e.M2EUtilities.java

License:Apache License

/** Maven version parser.
 *
 * @param version - the version string.//from   w ww .  ja  v  a2s  .c om
 * @return the version.
 */
public static Version parseMavenVersion(String version) {
    if (Strings.isNullOrEmpty(version)) {
        return new Version(0, 0, 0);
    }

    // Detect the snapshot
    final boolean isSnapshot;
    final String coreVersion;
    Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(version);
    if (matcher.matches()) {
        coreVersion = matcher.group(1);
        isSnapshot = true;
    } else {
        matcher = SNAPSHOT_VERSION_PATTERN.matcher(version);
        if (matcher.matches()) {
            coreVersion = matcher.group(1);
            isSnapshot = true;
        } else {
            coreVersion = version;
            isSnapshot = false;
        }
    }

    // Parse the numbers
    final String[] parts = coreVersion.split("[.]"); //$NON-NLS-1$
    final int[] numbers = new int[] { 0, 0, 0 };
    int i = 0;
    while (i < numbers.length && i < parts.length) {
        try {
            numbers[i] = Integer.parseInt(parts[i]);
            ++i;
        } catch (Exception exception) {
            // Force the exit of the loop since a number cannot be find.
            i = numbers.length;
        }
    }
    // Reply
    if (isSnapshot) {
        return new Version(numbers[0], numbers[1], numbers[2], SNAPSHOT_QUALIFIER);
    }
    return new Version(numbers[0], numbers[1], numbers[2]);
}

From source file:org.apache.archiva.converter.artifact.LegacyToDefaultConverter.java

License:Apache License

@Override
public void convert(Artifact artifact, ArtifactRepository targetRepository) throws ArtifactConversionException {
    if (artifact.getRepository().getUrl().equals(targetRepository.getUrl())) {
        throw new ArtifactConversionException(Messages.getString("exception.repositories.match")); //$NON-NLS-1$
    }//ww w.ja v a 2  s  .com

    if (!validateMetadata(artifact)) {
        addWarning(artifact, Messages.getString("unable.to.validate.metadata")); //$NON-NLS-1$
        return;
    }

    FileTransaction transaction = new FileTransaction();

    if (!copyPom(artifact, targetRepository, transaction)) {
        addWarning(artifact, Messages.getString("unable.to.copy.pom")); //$NON-NLS-1$
        return;
    }

    if (!copyArtifact(artifact, targetRepository, transaction)) {
        addWarning(artifact, Messages.getString("unable.to.copy.artifact")); //$NON-NLS-1$
        return;
    }

    Metadata metadata = createBaseMetadata(artifact);
    Versioning versioning = new Versioning();
    versioning.addVersion(artifact.getBaseVersion());
    metadata.setVersioning(versioning);
    updateMetadata(new ArtifactRepositoryMetadata(artifact), targetRepository, metadata, transaction);

    metadata = createBaseMetadata(artifact);
    metadata.setVersion(artifact.getBaseVersion());
    versioning = new Versioning();

    Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion());
    if (matcher.matches()) {
        Snapshot snapshot = new Snapshot();
        snapshot.setBuildNumber(Integer.parseInt(matcher.group(3)));
        snapshot.setTimestamp(matcher.group(2));
        versioning.setSnapshot(snapshot);
    }

    // TODO: merge latest/release/snapshot from source instead
    metadata.setVersioning(versioning);
    updateMetadata(new SnapshotArtifactRepositoryMetadata(artifact), targetRepository, metadata, transaction);

    if (!dryrun) {
        try {
            transaction.commit();
        } catch (TransactionException e) {
            throw new ArtifactConversionException(Messages.getString("transaction.failure", e.getMessage()), e); //$NON-NLS-1$
        }
    }
}

From source file:org.apache.archiva.converter.artifact.LegacyToDefaultConverter.java

License:Apache License

@SuppressWarnings("unchecked")
private boolean validateMetadata(Metadata metadata, RepositoryMetadata repositoryMetadata, Artifact artifact) {
    String groupIdKey;/*w ww.  jav a 2s.com*/
    String artifactIdKey = null;
    String snapshotKey = null;
    String versionKey = null;
    String versionsKey = null;

    if (repositoryMetadata.storedInGroupDirectory()) {
        groupIdKey = "failure.incorrect.groupMetadata.groupId"; //$NON-NLS-1$
    } else if (repositoryMetadata.storedInArtifactVersionDirectory()) {
        groupIdKey = "failure.incorrect.snapshotMetadata.groupId"; //$NON-NLS-1$
        artifactIdKey = "failure.incorrect.snapshotMetadata.artifactId"; //$NON-NLS-1$
        versionKey = "failure.incorrect.snapshotMetadata.version"; //$NON-NLS-1$
        snapshotKey = "failure.incorrect.snapshotMetadata.snapshot"; //$NON-NLS-1$
    } else {
        groupIdKey = "failure.incorrect.artifactMetadata.groupId"; //$NON-NLS-1$
        artifactIdKey = "failure.incorrect.artifactMetadata.artifactId"; //$NON-NLS-1$
        versionsKey = "failure.incorrect.artifactMetadata.versions"; //$NON-NLS-1$
    }

    boolean result = true;

    if (metadata.getGroupId() == null || !metadata.getGroupId().equals(artifact.getGroupId())) {
        addWarning(artifact, Messages.getString(groupIdKey));
        result = false;
    }
    if (!repositoryMetadata.storedInGroupDirectory()) {
        if (metadata.getGroupId() == null || !metadata.getArtifactId().equals(artifact.getArtifactId())) {
            addWarning(artifact, Messages.getString(artifactIdKey));
            result = false;
        }
        if (!repositoryMetadata.storedInArtifactVersionDirectory()) {
            // artifact metadata

            boolean foundVersion = false;
            if (metadata.getVersioning() != null) {
                for (String version : (List<String>) metadata.getVersioning().getVersions()) {
                    if (version.equals(artifact.getBaseVersion())) {
                        foundVersion = true;
                        break;
                    }
                }
            }

            if (!foundVersion) {
                addWarning(artifact, Messages.getString(versionsKey));
                result = false;
            }
        } else {
            // snapshot metadata
            if (!artifact.getBaseVersion().equals(metadata.getVersion())) {
                addWarning(artifact, Messages.getString(versionKey));
                result = false;
            }

            if (artifact.isSnapshot()) {
                Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion());
                if (matcher.matches()) {
                    boolean correct = false;
                    if (metadata.getVersioning() != null && metadata.getVersioning().getSnapshot() != null) {
                        Snapshot snapshot = metadata.getVersioning().getSnapshot();
                        int build = Integer.parseInt(matcher.group(3));
                        String ts = matcher.group(2);
                        if (build == snapshot.getBuildNumber() && ts.equals(snapshot.getTimestamp())) {
                            correct = true;
                        }
                    }

                    if (!correct) {
                        addWarning(artifact, Messages.getString(snapshotKey));
                        result = false;
                    }
                }
            }
        }
    }
    return result;
}

From source file:org.eclipse.che.maven.CheArtifactResolver.java

License:Apache License

private void resolveOrig(Artifact artifact, List<ArtifactRepository> remoteRepositories,
        RepositorySystemSession session) throws ArtifactResolutionException, ArtifactNotFoundException {
    if (artifact == null) {
        return;// w  ww .  ja va2s  .c  om
    }

    if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
        File systemFile = artifact.getFile();

        if (systemFile == null) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " has no file attached",
                    artifact);
        }

        if (!systemFile.exists()) {
            throw new ArtifactNotFoundException(
                    "System artifact: " + artifact + " not found in path: " + systemFile, artifact);
        }

        if (!systemFile.isFile()) {
            throw new ArtifactNotFoundException(
                    "System artifact: " + artifact + " is not a file: " + systemFile, artifact);
        }

        artifact.setResolved(true);

        return;
    }

    if (!artifact.isResolved()) {
        ArtifactResult result;

        try {
            ArtifactRequest artifactRequest = new ArtifactRequest();
            artifactRequest.setArtifact(RepositoryUtils.toArtifact(artifact));
            artifactRequest.setRepositories(RepositoryUtils.toRepos(remoteRepositories));

            // Maven 2.x quirk: an artifact always points at the local repo, regardless whether resolved or not
            LocalRepositoryManager lrm = session.getLocalRepositoryManager();
            String path = lrm.getPathForLocalArtifact(artifactRequest.getArtifact());
            artifact.setFile(new File(lrm.getRepository().getBasedir(), path));

            result = repoSystem.resolveArtifact(session, artifactRequest);
        } catch (org.eclipse.aether.resolution.ArtifactResolutionException e) {
            if (e.getCause() instanceof org.eclipse.aether.transfer.ArtifactNotFoundException) {
                throw new ArtifactNotFoundException(e.getMessage(), artifact.getGroupId(),
                        artifact.getArtifactId(), artifact.getVersion(), artifact.getType(),
                        artifact.getClassifier(), remoteRepositories, artifact.getDownloadUrl(),
                        artifact.getDependencyTrail(), e);
            } else {
                throw new ArtifactResolutionException(e.getMessage(), artifact, remoteRepositories, e);
            }
        }

        artifact.selectVersion(result.getArtifact().getVersion());
        artifact.setFile(result.getArtifact().getFile());
        artifact.setResolved(true);

        if (artifact.isSnapshot()) {
            Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion());
            if (matcher.matches()) {
                Snapshot snapshot = new Snapshot();
                snapshot.setTimestamp(matcher.group(2));
                try {
                    snapshot.setBuildNumber(Integer.parseInt(matcher.group(3)));
                    artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot));
                } catch (NumberFormatException e) {
                    logger.warn("Invalid artifact version " + artifact.getVersion() + ": " + e.getMessage());
                }
            }
        }
    }
}

From source file:org.jetbrains.idea.maven.server.embedder.CustomMaven32ArtifactResolver.java

License:Apache License

private void resolveOld(Artifact artifact, List<ArtifactRepository> remoteRepositories,
        RepositorySystemSession session) throws ArtifactResolutionException, ArtifactNotFoundException {
    if (artifact == null) {
        return;//from ww  w .  j a va 2 s.c om
    }

    if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
        File systemFile = artifact.getFile();

        if (systemFile == null) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " has no file attached",
                    artifact);
        }

        if (!systemFile.exists()) {
            throw new ArtifactNotFoundException(
                    "System artifact: " + artifact + " not found in path: " + systemFile, artifact);
        }

        if (!systemFile.isFile()) {
            throw new ArtifactNotFoundException(
                    "System artifact: " + artifact + " is not a file: " + systemFile, artifact);
        }

        artifact.setResolved(true);

        return;
    }

    if (!artifact.isResolved()) {
        ArtifactResult result;

        try {
            ArtifactRequest artifactRequest = new ArtifactRequest();
            artifactRequest.setArtifact(RepositoryUtils.toArtifact(artifact));
            artifactRequest.setRepositories(RepositoryUtils.toRepos(remoteRepositories));

            // Maven 2.x quirk: an artifact always points at the local repo, regardless whether resolved or not
            LocalRepositoryManager lrm = session.getLocalRepositoryManager();
            String path = lrm.getPathForLocalArtifact(artifactRequest.getArtifact());
            artifact.setFile(new File(lrm.getRepository().getBasedir(), path));

            result = repoSystem.resolveArtifact(session, artifactRequest);
        } catch (org.eclipse.aether.resolution.ArtifactResolutionException e) {
            if (e.getCause() instanceof org.eclipse.aether.transfer.ArtifactNotFoundException) {
                throw new ArtifactNotFoundException(e.getMessage(), artifact.getGroupId(),
                        artifact.getArtifactId(), artifact.getVersion(), artifact.getType(),
                        artifact.getClassifier(), remoteRepositories, artifact.getDownloadUrl(),
                        artifact.getDependencyTrail(), e);
            } else {
                throw new ArtifactResolutionException(e.getMessage(), artifact, remoteRepositories, e);
            }
        }

        artifact.selectVersion(result.getArtifact().getVersion());
        artifact.setFile(result.getArtifact().getFile());
        artifact.setResolved(true);

        if (artifact.isSnapshot()) {
            Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion());
            if (matcher.matches()) {
                Snapshot snapshot = new Snapshot();
                snapshot.setTimestamp(matcher.group(2));
                try {
                    snapshot.setBuildNumber(Integer.parseInt(matcher.group(3)));
                    artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot));
                } catch (NumberFormatException e) {
                    logger.warn("Invalid artifact version " + artifact.getVersion() + ": " + e.getMessage());
                }
            }
        }
    }
}