String getBundleClasspath() throws MojoExecutionException { StringBuffer sb = new StringBuffer(); File outputDirectory = new File(project.getBuild().getOutputDirectory()); if (outputDirectory.exists()) { if (classifier != null) { for (Iterator i = project.getAttachedArtifacts().iterator(); i.hasNext(); ) { Artifact a = (Artifact) i.next(); if (classifier.equals(a.getClassifier())) { sb.append(jars + "/" + a.getFile().getName()); } } } else if (projectJar != null) { sb.append(jars + "/" + projectJar); } } if (bundleClasspath != null) { for (int i = 0; i < bundleClasspath.length; i++) { if (sb.length() > 0) sb.append(','); sb.append(bundleClasspath[i]); } } for (Iterator i = getIncludedArtifacts().iterator(); i.hasNext(); ) { Artifact a = (Artifact) i.next(); if (sb.length() > 0) sb.append(','); sb.append(jars + "/" + a.getFile().getName()); } return sb.toString(); }
/** * Creates a list of all artifacts for the build. * * @return a list of all artifacts for the build including the attached ones. */ List<Artifact> getListOfArtifacts() { final MavenProject project = getProject(); final List<Artifact> artifacts = new ArrayList<Artifact>(project.getAttachedArtifacts()); final String packaging = project.getPackaging(); // POMs return null as their artifact, which will crash the transformation lateron. if (!"pom".equals(packaging)) { artifacts.add(project.getArtifact()); } return artifacts; }
/** * Returns a map from classifiers to artifact files of the given project. The classifier <code> * null</code> is mapped to the project's main artifact. */ private static Map<String, File> getAllProjectArtifacts(MavenProject project) { Map<String, File> artifacts = new HashMap<String, File>(); Artifact mainArtifact = project.getArtifact(); if (mainArtifact != null) { artifacts.put(null, mainArtifact.getFile()); } for (Artifact attachedArtifact : project.getAttachedArtifacts()) { artifacts.put(attachedArtifact.getClassifier(), attachedArtifact.getFile()); } return artifacts; }
@SuppressWarnings("unchecked") private Set<Artifact> getFilteredArtifacts( String groupId, String artifactId, String type, String classifier) throws MojoExecutionException { Set<Artifact> projectArtifacts = new LinkedHashSet<Artifact>(); projectArtifacts.addAll(project.getAttachedArtifacts()); projectArtifacts.addAll(project.getArtifacts()); FilterArtifacts filter = getFilters(groupId, artifactId, type, classifier); return filtterArtifacts(projectArtifacts, filter); }
public Set<Artifact> getNativeDependenciesArtifacts(File unpackDirectory, boolean sharedLibraries) throws MojoExecutionException { final Set<Artifact> filteredArtifacts = new LinkedHashSet<Artifact>(); // Add all dependent artifacts declared in the pom file @SuppressWarnings("unchecked") final Set<Artifact> allArtifacts = project.getDependencyArtifacts(); // Add all attached artifacts as well - this could come from the NDK mojo for example boolean result = allArtifacts.addAll(project.getAttachedArtifacts()); for (Artifact artifact : allArtifacts) { // A null value in the scope indicates that the artifact has been attached // as part of a previous build step (NDK mojo) if (isNativeLibrary(sharedLibraries, artifact.getType()) && artifact.getScope() == null) { // Including attached artifact log.debug( "Including attached artifact: " + artifact.getArtifactId() + "(" + artifact.getGroupId() + ")"); filteredArtifacts.add(artifact); } else if (isNativeLibrary(sharedLibraries, artifact.getType()) && (Artifact.SCOPE_COMPILE.equals(artifact.getScope()) || Artifact.SCOPE_RUNTIME.equals(artifact.getScope()))) { filteredArtifacts.add(artifact); } else if (APKLIB.equals(artifact.getType())) { // Check if the artifact contains a libs folder - if so, include it in the list File libsFolder = new File( AbstractAndroidMojo.getLibraryUnpackDirectory(unpackDirectory, artifact) + "/libs"); if (libsFolder.exists()) { filteredArtifacts.add(artifact); } } } Set<Artifact> transientArtifacts = processTransientDependencies(project.getDependencies(), sharedLibraries); filteredArtifacts.addAll(transientArtifacts); return filteredArtifacts; }
protected Set<Artifact> resolveDependencyArtifacts(final DependencySet dependencySet) throws InvalidAssemblerConfigurationException { final Set<Artifact> dependencyArtifacts = new LinkedHashSet<Artifact>(); if (resolvedArtifacts != null) { dependencyArtifacts.addAll(resolvedArtifacts); } if (dependencySet.isUseProjectArtifact()) { final Artifact projectArtifact = project.getArtifact(); if ((projectArtifact != null) && (projectArtifact.getFile() != null)) { dependencyArtifacts.add(projectArtifact); } else { logger.warn( "Cannot include project artifact: " + projectArtifact + "; it doesn't have an associated file or directory."); } } if (dependencySet.isUseProjectAttachments()) { @SuppressWarnings("unchecked") final List<Artifact> attachments = project.getAttachedArtifacts(); if (attachments != null) { for (final Iterator<Artifact> attachmentIt = attachments.iterator(); attachmentIt.hasNext(); ) { final Artifact attachment = attachmentIt.next(); if (attachment.getFile() != null) { dependencyArtifacts.add(attachment); } else { logger.warn( "Cannot include attached artifact: " + project.getId() + " for project: " + project.getId() + "; it doesn't have an associated file or directory."); } } } } if (dependencySet.isUseTransitiveFiltering()) { logger.debug("Filtering dependency artifacts USING transitive dependency path information."); } else { logger.debug( "Filtering dependency artifacts WITHOUT transitive dependency path information."); } final ScopeArtifactFilter filter = new ScopeArtifactFilter(dependencySet.getScope()); FilterUtils.filterArtifacts( dependencyArtifacts, dependencySet.getIncludes(), dependencySet.getExcludes(), dependencySet.isUseStrictFiltering(), dependencySet.isUseTransitiveFiltering(), logger, filter); return dependencyArtifacts; }
protected void attachP2Metadata() throws MojoExecutionException { if (!attachP2Metadata || !supportedProjectTypes.contains(project.getPackaging())) { return; } File file = project.getArtifact().getFile(); if (file == null || !file.canRead()) { throw new IllegalStateException(); } File targetDir = new File(project.getBuild().getDirectory()); Map<String, IArtifactFacade> artifactsToBeAttached = new HashMap<String, IArtifactFacade>(); ArtifactFacade projectDefaultArtifact = new ArtifactFacade(project.getArtifact()); Artifact p2contentArtifact = createP2Artifact( projectDefaultArtifact, EXTENSION_P2_METADATA, CLASSIFIER_P2_METADATA, FILE_NAME_P2_METADATA, targetDir); artifactsToBeAttached.put(CLASSIFIER_P2_METADATA, new ArtifactFacade(p2contentArtifact)); Artifact p2artifactsArtifact = createP2Artifact( projectDefaultArtifact, EXTENSION_P2_ARTIFACTS, CLASSIFIER_P2_ARTIFACTS, FILE_NAME_P2_ARTIFACTS, targetDir); artifactsToBeAttached.put(CLASSIFIER_P2_ARTIFACTS, new ArtifactFacade(p2artifactsArtifact)); try { List<IArtifactFacade> artifacts = new ArrayList<IArtifactFacade>(); artifacts.add(projectDefaultArtifact); for (Artifact attachedArtifact : project.getAttachedArtifacts()) { if (attachedArtifact.getFile() != null && attachedArtifact.getFile().getName().endsWith(".jar")) { artifacts.add(new ArtifactFacade(attachedArtifact)); } } Map<String, Set<Object>> generateMetadata = getP2Generator().generateMetadata(artifacts, artifactsToBeAttached, targetDir); ReactorProject reactorProject = DefaultReactorProject.adapt(project); for (Map.Entry<String, Set<Object>> entry : generateMetadata.entrySet()) { reactorProject.setDependencyMetadata(entry.getKey(), true, entry.getValue()); reactorProject.setDependencyMetadata(entry.getKey(), false, Collections.emptySet()); } } catch (IOException e) { throw new MojoExecutionException("Could not generate P2 metadata", e); } for (Entry<String, IArtifactFacade> entry : artifactsToBeAttached.entrySet()) { IArtifactFacade artifactFacade = entry.getValue(); projectHelper.attachArtifact( project, artifactFacade.getPackagingType(), artifactFacade.getClassifier(), artifactFacade.getLocation()); } File localArtifactsFile = new File(project.getBuild().getDirectory(), FILE_NAME_LOCAL_ARTIFACTS); writeArtifactLocations(localArtifactsFile, getAllProjectArtifacts(project)); }
protected void generateAggregatedZip( MavenProject rootProject, List<MavenProject> reactorProjects, Set<MavenProject> pomZipProjects) throws IOException, MojoExecutionException { File projectBaseDir = rootProject.getBasedir(); String rootProjectGroupId = rootProject.getGroupId(); String rootProjectArtifactId = rootProject.getArtifactId(); String rootProjectVersion = rootProject.getVersion(); String aggregatedZipFileName = "target/" + rootProjectArtifactId + "-" + rootProjectVersion + "-app.zip"; File projectOutputFile = new File(projectBaseDir, aggregatedZipFileName); getLog() .info( "Generating " + projectOutputFile.getAbsolutePath() + " from root project " + rootProjectArtifactId); File projectBuildDir = new File(projectBaseDir, reactorProjectOutputPath); if (projectOutputFile.exists()) { projectOutputFile.delete(); } createAggregatedZip( projectBaseDir, projectBuildDir, reactorProjectOutputPath, projectOutputFile, includeReadMe, pomZipProjects); if (rootProject.getAttachedArtifacts() != null) { // need to remove existing as otherwise we get a WARN Artifact found = null; for (Artifact artifact : rootProject.getAttachedArtifacts()) { if (artifactClassifier != null && artifact.hasClassifier() && artifact.getClassifier().equals(artifactClassifier)) { found = artifact; break; } } if (found != null) { rootProject.getAttachedArtifacts().remove(found); } } getLog() .info( "Attaching aggregated zip " + projectOutputFile + " to root project " + rootProject.getArtifactId()); projectHelper.attachArtifact(rootProject, artifactType, artifactClassifier, projectOutputFile); // if we are doing an install goal, then also install the aggregated zip manually // as maven will install the root project first, and then build the reactor projects, and at // this point // it does not help to attach artifact to root project, as those artifacts will not be installed // so we need to install manually List<String> activeProfileIds = new ArrayList<>(); List<Profile> activeProfiles = rootProject.getActiveProfiles(); if (activeProfiles != null) { for (Profile profile : activeProfiles) { String id = profile.getId(); if (Strings.isNotBlank(id)) { activeProfileIds.add(id); } } } if (rootProject.hasLifecyclePhase("install")) { getLog().info("Installing aggregated zip " + projectOutputFile); InvocationRequest request = new DefaultInvocationRequest(); request.setBaseDirectory(rootProject.getBasedir()); request.setPomFile(new File("./pom.xml")); request.setGoals(Collections.singletonList("install:install-file")); request.setRecursive(false); request.setInteractive(false); request.setProfiles(activeProfileIds); Properties props = new Properties(); props.setProperty("file", aggregatedZipFileName); props.setProperty("groupId", rootProjectGroupId); props.setProperty("artifactId", rootProjectArtifactId); props.setProperty("version", rootProjectVersion); props.setProperty("classifier", "app"); props.setProperty("packaging", "zip"); props.setProperty("generatePom", "false"); request.setProperties(props); getLog() .info( "Installing aggregated zip using: mvn install:install-file" + serializeMvnProperties(props)); Invoker invoker = new DefaultInvoker(); try { InvocationResult result = invoker.execute(request); if (result.getExitCode() != 0) { throw new IllegalStateException("Error invoking Maven goal install:install-file"); } } catch (MavenInvocationException e) { throw new MojoExecutionException("Error invoking Maven goal install:install-file", e); } } if (rootProject.hasLifecyclePhase("deploy")) { if (deploymentRepository == null && Strings.isNullOrBlank(altDeploymentRepository)) { String msg = "Cannot run deploy phase as Maven project has no <distributionManagement> with the maven url to use for deploying the aggregated zip file, neither an altDeploymentRepository property."; getLog().warn(msg); throw new MojoExecutionException(msg); } getLog() .info( "Deploying aggregated zip " + projectOutputFile + " to root project " + rootProject.getArtifactId()); getLog() .info( "Using deploy goal: " + deployFileGoal + " with active profiles: " + activeProfileIds); InvocationRequest request = new DefaultInvocationRequest(); request.setBaseDirectory(rootProject.getBasedir()); request.setPomFile(new File("./pom.xml")); request.setGoals(Collections.singletonList(deployFileGoal)); request.setRecursive(false); request.setInteractive(false); request.setProfiles(activeProfileIds); request.setProperties(getProject().getProperties()); Properties props = new Properties(); props.setProperty("file", aggregatedZipFileName); props.setProperty("groupId", rootProjectGroupId); props.setProperty("artifactId", rootProjectArtifactId); props.setProperty("version", rootProjectVersion); props.setProperty("classifier", "app"); props.setProperty("packaging", "zip"); String deployUrl = null; if (!Strings.isNullOrBlank(deployFileUrl)) { deployUrl = deployFileUrl; } else if (altDeploymentRepository != null && altDeploymentRepository.contains("::")) { deployUrl = altDeploymentRepository.substring(altDeploymentRepository.lastIndexOf("::") + 2); } else { deployUrl = deploymentRepository.getUrl(); } props.setProperty("url", deployUrl); props.setProperty("repositoryId", deploymentRepository.getId()); props.setProperty("generatePom", "false"); request.setProperties(props); getLog() .info( "Deploying aggregated zip using: mvn deploy:deploy-file" + serializeMvnProperties(props)); Invoker invoker = new DefaultInvoker(); try { InvocationResult result = invoker.execute(request); if (result.getExitCode() != 0) { throw new IllegalStateException("Error invoking Maven goal deploy:deploy-file"); } } catch (MavenInvocationException e) { throw new MojoExecutionException("Error invoking Maven goal deploy:deploy-file", e); } } }
public Set<Artifact> getNativeDependenciesArtifacts( AbstractAndroidMojo mojo, File unpackDirectory, boolean sharedLibraries) throws MojoExecutionException { log.debug( "Finding native dependencies. UnpackFolder=" + unpackDirectory + " shared=" + sharedLibraries); final Set<Artifact> filteredArtifacts = new LinkedHashSet<Artifact>(); final Set<Artifact> allArtifacts = new LinkedHashSet<Artifact>(); // Add all dependent artifacts declared in the pom file // Note: The result of project.getDependencyArtifacts() can be an UnmodifiableSet so we // have created our own above and add to that. allArtifacts.addAll(project.getDependencyArtifacts()); // Add all attached artifacts as well - this could come from the NDK mojo for example allArtifacts.addAll(project.getAttachedArtifacts()); // Add all transitive artifacts as well // this allows armeabi classifier -> apklib -> apklib -> apk chaining to only include armeabi in // APK allArtifacts.addAll(project.getArtifacts()); for (Artifact artifact : allArtifacts) { log.debug("Checking artifact : " + artifact); // A null value in the scope indicates that the artifact has been attached // as part of a previous build step (NDK mojo) if (isNativeLibrary(sharedLibraries, artifact.getType()) && artifact.getScope() == null) { // Including attached artifact log.debug("Including attached artifact: " + artifact + ". Artifact scope is not set."); filteredArtifacts.add(artifact); } else { if (isNativeLibrary(sharedLibraries, artifact.getType()) && (Artifact.SCOPE_COMPILE.equals(artifact.getScope()) || Artifact.SCOPE_RUNTIME.equals(artifact.getScope()))) { log.debug( "Including attached artifact: " + artifact + ". Artifact scope is Compile or Runtime."); filteredArtifacts.add(artifact); } else { final String type = artifact.getType(); if (APKLIB.equals(type)) { // Check if the artifact contains a libs folder - if so, include it in the list File libsFolder = null; if (mojo != null) { libsFolder = mojo.getUnpackedLibNativesFolder(artifact); } else { // This is used from NativeHelperTest since we have no AbstractAndroidMojo there libsFolder = new File( AbstractAndroidMojo.getLibraryUnpackDirectory(unpackDirectory, artifact), "libs"); } if (!libsFolder.exists()) { log.debug("Skipping " + libsFolder.getAbsolutePath() + " for native artifacts"); continue; } if (!libsFolder.isDirectory()) { continue; } log.debug("Checking " + libsFolder.getAbsolutePath() + " for native artifacts"); // make sure we ignore libs folders that only contain JARs // The regular expression filters out all file paths ending with '.jar' or '.JAR', // so all native libs remain if (libsFolder.list(new PatternFilenameFilter("^.*(?<!(?i)\\.jar)$")).length > 0) { log.debug("Including attached artifact: " + artifact + ". Artifact is APKLIB."); filteredArtifacts.add(artifact); } } else if (!"jar".equals(type)) { log.debug("Not checking " + type + " for native artifacts"); } } } } Set<Artifact> transitiveArtifacts = processTransitiveDependencies(project.getDependencies(), sharedLibraries); filteredArtifacts.addAll(transitiveArtifacts); return filteredArtifacts; }
public Set<Artifact> getNativeDependenciesArtifacts(File unpackDirectory, boolean sharedLibraries) throws MojoExecutionException { final Set<Artifact> filteredArtifacts = new LinkedHashSet<Artifact>(); final Set<Artifact> allArtifacts = new LinkedHashSet<Artifact>(); // Add all dependent artifacts declared in the pom file // Note: The result of project.getDependencyArtifacts() can be an UnmodifiableSet so we // have created our own above and add to that. allArtifacts.addAll(project.getDependencyArtifacts()); // Add all attached artifacts as well - this could come from the NDK mojo for example allArtifacts.addAll(project.getAttachedArtifacts()); for (Artifact artifact : allArtifacts) { // A null value in the scope indicates that the artifact has been attached // as part of a previous build step (NDK mojo) if (isNativeLibrary(sharedLibraries, artifact.getType()) && artifact.getScope() == null) { // Including attached artifact log.debug( "Including attached artifact: " + artifact.getArtifactId() + "(" + artifact.getGroupId() + "). Artifact scope is not set."); filteredArtifacts.add(artifact); } else { if (isNativeLibrary(sharedLibraries, artifact.getType()) && (Artifact.SCOPE_COMPILE.equals(artifact.getScope()) || Artifact.SCOPE_RUNTIME.equals(artifact.getScope()))) { log.debug( "Including attached artifact: " + artifact.getArtifactId() + "(" + artifact.getGroupId() + "). Artifact scope is Compile or Runtime."); filteredArtifacts.add(artifact); } else { if (APKLIB.equals(artifact.getType())) { // Check if the artifact contains a libs folder - if so, include it in the list File libsFolder = new File( AbstractAndroidMojo.getLibraryUnpackDirectory(unpackDirectory, artifact), "libs"); // make sure we ignore libs folders that only contain JARs // The regular expression filters out all file paths ending with '.jar' or '.JAR', // so all native libs remain if (libsFolder.exists() && libsFolder.list(new PatternFilenameFilter("^.*(?<!(?i)\\.jar)$")).length > 0) { log.debug( "Including attached artifact: " + artifact.getArtifactId() + "(" + artifact.getGroupId() + "). Artifact is APKLIB."); filteredArtifacts.add(artifact); } } } } } Set<Artifact> transientArtifacts = processTransientDependencies(project.getDependencies(), sharedLibraries); filteredArtifacts.addAll(transientArtifacts); return filteredArtifacts; }