Example usage for org.apache.maven.artifact.repository ArtifactRepositoryPolicy UPDATE_POLICY_ALWAYS

List of usage examples for org.apache.maven.artifact.repository ArtifactRepositoryPolicy UPDATE_POLICY_ALWAYS

Introduction

In this page you can find the example usage for org.apache.maven.artifact.repository ArtifactRepositoryPolicy UPDATE_POLICY_ALWAYS.

Prototype

String UPDATE_POLICY_ALWAYS

To view the source code for org.apache.maven.artifact.repository ArtifactRepositoryPolicy UPDATE_POLICY_ALWAYS.

Click Source Link

Usage

From source file:com.github.wcy123.maven.launcher.RunnerMojo.java

License:Apache License

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (isSkip()) {
        getLog().info("Skipping plugin execution");
        return;/*from   w  ww  .j ava2s  .  com*/
    }

    if (coordinate.getArtifactId() == null && artifact == null) {
        throw new MojoFailureException("You must specify an artifact, "
                + "e.g. -Dartifact=org.apache.maven.plugins:maven-downloader-plugin:1.0");
    }
    if (artifact != null) {
        String[] tokens = StringUtils.split(artifact, ":");
        if (tokens.length < 3 || tokens.length > 5) {
            throw new MojoFailureException(
                    "Invalid artifact, you must specify groupId:artifactId:version[:packaging[:classifier]] "
                            + artifact);
        }
        coordinate.setGroupId(tokens[0]);
        coordinate.setArtifactId(tokens[1]);
        coordinate.setVersion(tokens[2]);
        if (tokens.length >= 4) {
            coordinate.setType(tokens[3]);
        }
        if (tokens.length == 5) {
            coordinate.setClassifier(tokens[4]);
        }
    }

    ArtifactRepositoryPolicy always = new ArtifactRepositoryPolicy(true,
            ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);

    List<ArtifactRepository> repoList = new ArrayList<ArtifactRepository>();

    if (pomRemoteRepositories != null) {
        repoList.addAll(pomRemoteRepositories);
    }

    if (remoteRepositories != null) {
        // Use the same format as in the deploy plugin id::layout::url
        List<String> repos = Arrays.asList(StringUtils.split(remoteRepositories, ","));
        for (String repo : repos) {
            repoList.add(parseRepository(repo, always));
        }
    }

    try {
        ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(
                session.getProjectBuildingRequest());
        buildingRequest.setProject(session.getCurrentProject());
        buildingRequest.setRemoteRepositories(repoList);

        getLog().info("Resolving " + coordinate + " with transitive dependencies");

        // FIXME
        // artifactResolver.resolveArtifact( buildingRequest, coordinate );

        final Iterable<ArtifactResult> artifactResults = dependencyResolver.resolveDependencies(buildingRequest,
                coordinate, null);

        // start from here
        if (!artifactResults.iterator().hasNext()) {
            getLog().error("cannot find the first artifcat");
            return;
        }
        List<Artifact> listOfArtifacts = new ArrayList<>();
        for (ArtifactResult artifactResult : artifactResults) {
            getLog().info("artifacts " + artifactResult.getArtifact());
            listOfArtifacts.add(artifactResolver.resolveArtifact(buildingRequest, artifactResult.getArtifact())
                    .getArtifact());

        }
        URL[] urls = new URL[listOfArtifacts.size()];
        for (int i = 0; i < urls.length; ++i) {
            urls[i] = listOfArtifacts.get(i).getFile().toURI().toURL();
        }
        final SharedUrlClassLoader classLoader = SharedUrlClassLoader.create(urls);

        final JarFile jarFile = new JarFile(urls[0].getFile());
        final Manifest manifest = jarFile.getManifest();
        String value = manifest.getMainAttributes().getValue("Main-Class");
        // final String charSequence = "cannot find Main-Class: " +
        // manifest.getMainAttributes().entrySet().stream().map(e -> e.getKey() + ":" +
        // e.getValue()).collect(Collectors.joining("\n"));
        if (value == null) {
            throw new MojoFailureException("cannot find Main-Class " + classLoader.getURLs());
        }
        value = "com.wcy123.HelloWorldApplication";
        System.out.println("value is value " + value);
        final Class<?> aClass = Class.forName(value, true, classLoader);
        final Method main = aClass.getMethod("main", String[].class);
        Thread runnerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    main.invoke(null, new Object[] { new String[] { "hello", "world" } });
                } catch (IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        });
        runnerThread.setContextClassLoader(classLoader);
        runnerThread.start();
        runnerThread.join();
    } catch (DependencyResolverException e) {
        throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
    } catch (IOException e) {
        getLog().error("cannot create class loader", e);
        throw new MojoFailureException("cannot create class loader");
    } catch (ArtifactResolverException e) {
        getLog().error("cannot resolve artifact", e);
        throw new MojoFailureException("cannot resolve artifact");
    } catch (ClassNotFoundException e) {
        getLog().error("cannot load Main-Class", e);
        throw new MojoFailureException("cannot load Main-Class");
    } catch (NoSuchMethodException e) {
        getLog().error("cannot get main method", e);
        throw new MojoFailureException("cannot get main method");
    } catch (InterruptedException e) {
        getLog().error("interrupted", e);
    }
}

From source file:com.googlecode.jndiresources.maven.MavenEmbedder.java

License:Apache License

/**
 * Create a repository.//from   w  ww  . j a  va 2  s .c  o m
 * 
 * @param url The url of repository.
 * @param repositoryId The repository id.
 * 
 * @return The repository.
 * @throws ComponentLookupException If error.
 */
public ArtifactRepository createRepository(final String url, final String repositoryId)
        throws ComponentLookupException {

    final String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;

    final String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;

    final ArtifactRepositoryPolicy snapshotsPolicy = new ArtifactRepositoryPolicy(true, updatePolicyFlag,
            checksumPolicyFlag);

    final ArtifactRepositoryPolicy releasesPolicy = new ArtifactRepositoryPolicy(true, updatePolicyFlag,
            checksumPolicyFlag);

    return artifactRepositoryFactory_.createArtifactRepository(repositoryId, url,
            defaultArtifactRepositoryLayout_, snapshotsPolicy, releasesPolicy);
}

From source file:com.messapix.ftatr.jfxmobile.maven.plugin.android.AndroidConf.java

License:Apache License

private void configureRepos() throws MojoExecutionException {
    try {//from  www  .ja va 2  s .  com
        ArtifactRepositoryPolicy policy = new ArtifactRepositoryPolicy(true,
                ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);

        {
            File androidSupportRepositoryDir = androidSdkDir.resolve("extras/android/m2repository").toFile();

            if (!androidSupportRepositoryDir.exists() || !androidSupportRepositoryDir.isDirectory()) {
                throw new MojoExecutionException(
                        "Android Support Repository is not installed in your SDK. Please install it from Android SDK Manager");
            }

            ArtifactRepository ar = artifactRepositoryFactory.createArtifactRepository("jfxmobile-android-repo",
                    "file://" + androidSupportRepositoryDir, "default", null, policy);

            project.getRemoteArtifactRepositories().add(ar);
        }

        {
            File dir = androidSdkDir.resolve("extras/m2repository").toFile();

            if (dir.exists() && dir.isDirectory()) {
                ArtifactRepository ar = artifactRepositoryFactory.createArtifactRepository("jfxmobile-sdk-repo",
                        "file://" + dir, "default", null, policy);

                project.getRemoteArtifactRepositories().add(ar);
            }
        }

        {
            File dir = androidSdkDir.resolve("extras/google/m2repository").toFile();

            if (dir.exists() && dir.isDirectory()) {
                ArtifactRepository ar = artifactRepositoryFactory.createArtifactRepository(
                        "jfxmobile-google-repo", "file://" + dir, "default", null, policy);

                project.getRemoteArtifactRepositories().add(ar);
            }
        }
    } catch (UnknownRepositoryLayoutException ex) {
        // NOTHING
    }
}

From source file:com.opoopress.maven.plugins.plugin.downloader.DefaultArtifactDownloader.java

License:Apache License

private List<ArtifactRepository> getRemoteRepositoryList() throws MojoFailureException {
    List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>();
    if (remoteArtifactRepositories != null) {
        repositories.addAll(remoteArtifactRepositories);
    }/*from   w w  w .  j  av  a  2  s.c o m*/

    ArtifactRepositoryPolicy always = new ArtifactRepositoryPolicy(true,
            ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);

    if (remoteRepositories != null) {
        // Use the same format as in the deploy plugin id::layout::url
        List<String> repos = Arrays.asList(StringUtils.split(remoteRepositories, ","));
        for (String repo : repos) {
            repositories.add(parseRepository(repo, always));
        }
    }

    if (enableOpooPressRepos) {
        // Use the same format as in the deploy plugin id::layout::url
        ArtifactRepositoryPolicy never = new ArtifactRepositoryPolicy(true,
                ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
        repositories.add(parseRepository(OP_RELEASES_REPO, never));
        repositories.add(parseRepository(OP_SNAPSHOTS_REPO, always));
    }

    return repositories;
}

From source file:com.opoopress.maven.plugins.plugin.GetMojo.java

License:Apache License

/**
 *
 <dependency>/*www. j a  v  a2s. com*/
 <groupId>org.springframework</groupId>
 <artifactId>spring-test</artifactId>
 <version>${spring.version}</version>
 <scope>test</scope>
        
 * @throws MojoExecutionException
 * @throws MojoFailureException
 */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    String groupId = "org.springframework";
    String artifactId = "spring";
    String version = "2.5.6";
    String packaging = "jar";
    String classifier = "";

    groupId = "cn.redflagsoft";
    artifactId = "rfs-base";
    version = "2.1.17-SNAPSHOT";
    packaging = "jar";

    //        Artifact toDownload = classifier == null
    //                ? artifactFactory.createBuildArtifact( groupId, artifactId, version, packaging )
    //                : artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier );
    //        Artifact dummyOriginatingArtifact =
    //                artifactFactory.createBuildArtifact( "org.apache.maven.plugins", "maven-downloader-plugin", "1.0", "jar" );

    Artifact toDownload = artifactFactory.createBuildArtifact(groupId, artifactId, version, packaging);
    toDownload = artifactFactory.createArtifactWithClassifier("org.opoo.press.themes",
            "opoopress-theme-default", "1.2.0-SNAPSHOT", "jar", "opoopress-theme");

    Artifact dummyOriginatingArtifact = artifactFactory.createBuildArtifact("org.apache.maven.plugins",
            "maven-downloader-plugin", "1.0", "jar");
    ArtifactRepositoryPolicy always = new ArtifactRepositoryPolicy(true,
            ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);

    List<ArtifactRepository> repoList = new ArrayList<ArtifactRepository>();

    if (pomRemoteRepositories != null) {
        repoList.addAll(pomRemoteRepositories);
    }

    //        remoteRepositories = "rfs.snapshots::default::http://192.168.18.6/maven2/snapshots";

    if (remoteRepositories != null) {
        // Use the same format as in the deploy plugin id::layout::url
        List<String> repos = Arrays.asList(StringUtils.split(remoteRepositories, ","));
        for (String repo : repos) {
            repoList.add(parseRepository(repo, always));
        }
    }

    try {
        //            if ( transitive )
        //            {
        //                getLog().info( "Resolving " + toDownload + " with transitive dependencies" );
        //                artifactResolver.resolveTransitively( Collections.singleton(toDownload), dummyOriginatingArtifact,
        //                        repoList, localRepository, source );
        //            }
        //            else
        //            {
        //                getLog().info( "Resolving " + toDownload );
        //                artifactResolver.resolve( toDownload, repoList, localRepository );
        //            }

        getLog().info("Resolving " + toDownload);
        artifactResolver.resolve(toDownload, repoList, localRepository);
        System.out.println(toDownload);
        System.out.println(toDownload.getFile().exists());
    } catch (AbstractArtifactResolutionException e) {
        throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
    }

    //invokePostGoals("", "");

}

From source file:com.tenderowls.opensource.haxemojos.components.NativeBootstrap.java

License:Apache License

private void initializeHaxelib(File pluginHome) throws Exception {
    try {/*w w  w.j a  v a 2 s  . co m*/
        File haxelibHome = new File(pluginHome, "_haxelib");

        if (!haxelibHome.exists()) {
            // Setup haxelib
            haxelib.execute("setup", haxelibHome.getAbsolutePath());
        }
    } catch (NativeProgramException e) {
        throw new Exception("Cant setup haxelib", e);
    }

    // Add haxelib virtual repository.
    project.getRemoteArtifactRepositories()
            .add(new MavenArtifactRepository("lib.haxe.org", "http://lib.haxe.org",
                    new HaxelibRepositoryLayout(),
                    new ArtifactRepositoryPolicy(false, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
                            ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE),
                    new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
                            ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE)));
}

From source file:hudson.maven.MavenEmbedder.java

License:Apache License

public ArtifactRepository createRepository(String url, String repositoryId) throws ComponentLookupException {
    // snapshots vs releases
    // offline = to turning the update policy off

    //TODO: we'll need to allow finer grained creation of repositories but this will do for now

    String updatePolicyFlag = ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS;

    String checksumPolicyFlag = ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN;

    ArtifactRepositoryPolicy snapshotsPolicy = new ArtifactRepositoryPolicy(true, updatePolicyFlag,
            checksumPolicyFlag);//from w  ww.ja va 2s.  c o m

    ArtifactRepositoryPolicy releasesPolicy = new ArtifactRepositoryPolicy(true, updatePolicyFlag,
            checksumPolicyFlag);

    RepositorySystem repositorySystem = lookup(RepositorySystem.class);

    ArtifactRepositoryLayout repositoryLayout = lookup(ArtifactRepositoryLayout.class, "default");

    return repositorySystem.createArtifactRepository(repositoryId, url, repositoryLayout, snapshotsPolicy,
            releasesPolicy);

}

From source file:io.treefarm.plugins.haxe.components.NativeBootstrap.java

License:Apache License

private void initializeHaxelib(File pluginHome) throws Exception {
    // Add haxelib virtual repository.
    project.getRemoteArtifactRepositories()
            .add(new MavenArtifactRepository(HaxelibHelper.HAXELIB_URL, "http://" + HaxelibHelper.HAXELIB_URL,
                    new HaxelibRepositoryLayout(),
                    new ArtifactRepositoryPolicy(false, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
                            ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE),
                    new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
                            ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE)));
}

From source file:net.oneandone.maven.plugins.prerelease.util.IntegrationBase.java

License:Apache License

public static Maven maven(World world) {
    DefaultPlexusContainer container;/* ww w  . j  ava2  s.  c  om*/
    ArtifactRepositoryFactory factory;
    ArtifactRepository central;
    ArtifactRepository snapshots;
    FileNode localDir;
    ArtifactRepository local;
    MavenSession session;
    RepositorySystem repoSystem;
    DefaultRepositorySystemSession repoSession;

    container = container(null, null, Logger.LEVEL_DISABLED);
    try {
        factory = container.lookup(ArtifactRepositoryFactory.class);
    } catch (ComponentLookupException e) {
        throw new IllegalStateException(e);
    }
    central = factory.createArtifactRepository("central", "http://repo1.maven.org/maven2",
            new DefaultRepositoryLayout(),
            new ArtifactRepositoryPolicy(false, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
                    ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN),
            new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
                    ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN));
    snapshots = factory.createArtifactRepository("apache-snapshots", "http://repository.apache.org/snapshots/",
            new DefaultRepositoryLayout(),
            new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY,
                    ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN),
            new ArtifactRepositoryPolicy(false, ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
                    ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN));
    localDir = defaultLocalRepositoryDir(world);
    local = factory.createArtifactRepository("local", localDir.getURI().toASCIIString(),
            new DefaultRepositoryLayout(),
            new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS,
                    ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN),
            new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_NEVER,
                    ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN));

    try {
        repoSystem = container.lookup(RepositorySystem.class);
        repoSession = MavenRepositorySystemUtils.newSession();
        repoSession.setLocalRepositoryManager(
                repoSystem.newLocalRepositoryManager(repoSession, new LocalRepository(localDir.getAbsolute())));
        session = new MavenSession(container, repoSession, new DefaultMavenExecutionRequest(),
                new DefaultMavenExecutionResult());
        return new Maven(world, new DefaultLog(null), session, local, null, null,
                container.lookup(ProjectBuilder.class), Arrays.asList(central, snapshots));
    } catch (ComponentLookupException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.apache.sling.maven.bundlesupport.BundleInstallFileMojo.java

License:Apache License

@SuppressWarnings({ "rawtypes", "unchecked" })
private String resolveBundleFileFromArtifact() throws MojoExecutionException {
    if (artifactId == null && artifact == null) {
        return null;
    }/*  www .  j  a va 2  s  .c o m*/
    if (artifactId == null) {
        String[] tokens = StringUtils.split(artifact, ":");
        if (tokens.length != 3 && tokens.length != 4 && tokens.length != 5) {
            throw new MojoExecutionException("Invalid artifact, you must specify "
                    + "groupId:artifactId:version[:packaging[:classifier]] " + artifact);
        }
        groupId = tokens[0];
        artifactId = tokens[1];
        version = tokens[2];
        if (tokens.length >= 4)
            packaging = tokens[3];
        if (tokens.length == 5)
            classifier = tokens[4];
    }
    Artifact packageArtifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version,
            packaging, classifier);

    if (pomRemoteRepositories == null) {
        pomRemoteRepositories = new ArrayList();
    }

    List repoList = new ArrayList(pomRemoteRepositories);

    if (repositoryUrl != null) {
        ArtifactRepositoryPolicy policy = new ArtifactRepositoryPolicy(true,
                ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN);
        ArtifactRepository remoteRepo = artifactRepositoryFactory.createArtifactRepository(repositoryId,
                repositoryUrl, repositoryLayout, policy, policy);

        repoList.add(remoteRepo);
    }

    try {
        artifactResolver.resolve(packageArtifact, repoList, localRepository);
        getLog().info("Resolved artifact to " + packageArtifact.getFile().getAbsolutePath());
    } catch (AbstractArtifactResolutionException e) {
        throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e);
    }

    return packageArtifact.getFile().getAbsolutePath();
}