List of usage examples for org.apache.maven.project MavenProject getVersion
public String getVersion()
From source file:io.fabric8.maven.ZipMojo.java
License:Apache License
protected void generateZip() throws DependencyTreeBuilderException, MojoExecutionException, IOException, MojoFailureException { File appBuildDir = buildDir;/*from www . ja v a2 s.co m*/ if (Strings.isNotBlank(pathInZip)) { appBuildDir = new File(buildDir, pathInZip); } appBuildDir.mkdirs(); if (hasConfigDir()) { copyAppConfigFiles(appBuildDir, appConfigDir); } else { getLog().info("The app configuration files directory " + appConfigDir + " doesn't exist, so not copying any additional project documentation or configuration files"); } MavenProject project = getProject(); if (!ignoreProject) { File kubernetesJson = getKubernetesJson(); if (kubernetesJson != null && kubernetesJson.isFile() && kubernetesJson.exists()) { File jsonFile = new File(appBuildDir, "kubernetes.json"); jsonFile.getParentFile().mkdirs(); Files.copy(kubernetesJson, jsonFile); } // TODO if no iconRef is specified we could try guess based on the project? // lets check if we can use an icon reference copyIconToFolder(appBuildDir); } // lets only generate a app zip if we have a requirement (e.g. we're not a parent pom packaging project) and // we have defined some configuration files or dependencies // to avoid generating dummy apps for parent poms if (hasConfigDir() || !ignoreProject) { if (includeReadMe) { copyReadMe(appBuildDir); } if (generateSummaryFile) { copySummaryText(appBuildDir); } if (generateAppPropertiesFile) { String name = project.getName(); if (Strings.isNullOrBlank(name)) { name = project.getArtifactId(); } String description = project.getDescription(); Properties appProperties = new Properties(); appProperties.put("name", name); if (Strings.isNotBlank(description)) { appProperties.put("description", description); } appProperties.put("groupId", project.getGroupId()); appProperties.put("artifactId", project.getArtifactId()); appProperties.put("version", project.getVersion()); File appPropertiesFile = new File(appBuildDir, "fabric8.properties"); appPropertiesFile.getParentFile().mkdirs(); if (!appPropertiesFile.exists()) { appProperties.store(new FileWriter(appPropertiesFile), "Fabric8 Properties"); } } File outputZipFile = getZipFile(); File legalDir = null; if (includeLegal) { legalDir = new File(project.getBuild().getOutputDirectory(), "META-INF"); } Zips.createZipFile(getLog(), buildDir, outputZipFile, legalDir); projectHelper.attachArtifact(project, artifactType, artifactClassifier, outputZipFile); getLog().info("Created app zip file: " + outputZipFile); } }
From source file:io.fabric8.maven.ZipMojo.java
License:Apache License
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);//from w ww . j a v a2s . co m 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(getProjectAndFabric8Properties(getProject())); 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); } } }
From source file:io.github.wanghuayao.maven.plugin.oaktree.CreateTreeMojo.java
License:Apache License
public void execute() throws MojoExecutionException { log = getLog();// w ww . j a v a 2 s .co m try { OakProperties properties = new OakProperties(); dependencyArtifacts = "io.github.wanghuayao:maven-plugin-oaktree:" + properties.getVersion() + ":dependency-artifacts"; filter = new ProcessingFilter(likePatten, hitePatten, deep); MavenProject mp = (MavenProject) this.getPluginContext().get("project"); String mavenHomeStr = removeUnusedCharInPath(mavenHome.getPath()); File mvnExecFile = new File(mavenHomeStr + "/bin/mvn"); mvnExec = mvnExecFile.getPath(); String os = System.getProperty("os.name"); if (os.toLowerCase().startsWith("win")) { mvnExec = mvnExec + ".cmd"; isWindwos = true; } File okaBaseDir = new File(outputDirectory, "oaktree"); File okadependencyDir = new File(okaBaseDir, "dependency"); if (!okadependencyDir.exists()) { okadependencyDir.mkdirs(); } okadependencyOutputDir = okadependencyDir.getPath(); ArtifactItem rootItem = new ArtifactItem(mp.getGroupId(), mp.getArtifactId(), mp.getPackaging(), mp.getVersion()); File rootPom = mp.getFile(); int startDeep = 2; for (Object obj : mp.getDependencyArtifacts()) { DefaultArtifact artifact = (DefaultArtifact) obj; ArtifactItem chieldern = ArtifactItem.valueOf(artifact); if (filter.isGoOnProcessing(chieldern, startDeep)) { calcDependancy(chieldern, startDeep); rootItem.addChildren(chieldern); } } FileWriter w = null; try { File okaFile = new File(okaBaseDir, "okatree.txt"); if (okaFile.exists()) { okaFile.delete(); } w = new FileWriter(okaFile); log.info("writing file : " + okaFile.getPath()); printArtifactItem(w, rootItem, ""); log.info("writing complete."); } finally { StreamUtils.quiteClose(w); } } catch (Exception ex) { getLog().error(ex.getMessage(), ex); throw new MojoExecutionException(ex.getMessage(), ex); } }
From source file:io.github.wanghuayao.maven.plugin.oaktree.DependencyArtifactsMojo.java
License:Apache License
public void execute() throws MojoExecutionException { try {/* www . j a va 2s . c om*/ MavenProject mp = (MavenProject) this.getPluginContext().get("project"); if (outputFile == null) { outputFile = new File(outputDirectory, mp.getGroupId() + "." + mp.getArtifactId() + "." + mp.getVersion() + ".txt"); } // delete old file if (outputFile.exists()) { return; } FileWriter w = null; try { w = new FileWriter(outputFile); for (Object obj : mp.getDependencyArtifacts()) { DefaultArtifact artifact = (DefaultArtifact) obj; ArtifactWriter.artifactToWriter(artifact, w); w.write('\n'); } } catch (IOException e) { throw new MojoExecutionException("Error writing file " + outputFile, e); } finally { StreamUtils.quiteClose(w); } } catch (Exception ex) { ex.printStackTrace(); throw new MojoExecutionException(ex.getMessage(), ex); } }
From source file:io.reactiverse.vertx.maven.plugin.mojos.PackageMojo.java
License:Apache License
public static String computeOutputName(MavenProject project, String classifier) { String finalName = project.getBuild().getFinalName(); if (finalName != null) { if (finalName.endsWith(".jar")) { finalName = finalName.substring(0, finalName.length() - 4); }/*from w ww .j a v a 2s . c o m*/ if (classifier != null && !classifier.isEmpty()) { finalName += "-" + classifier; } finalName += ".jar"; return finalName; } else { finalName = project.getArtifactId() + "-" + project.getVersion(); if (classifier != null && !classifier.isEmpty()) { finalName += "-" + classifier; } finalName += ".jar"; return finalName; } }
From source file:io.sarl.maven.docs.AbstractDocumentationMojo.java
License:Apache License
private Properties createProjectProperties() { final Properties props = new Properties(); final MavenProject prj = this.session.getCurrentProject(); props.put("project.groupId", prj.getGroupId()); //$NON-NLS-1$ props.put("project.artifactId", prj.getArtifactId()); //$NON-NLS-1$ props.put("project.basedir", prj.getBasedir()); //$NON-NLS-1$ props.put("project.description", prj.getDescription()); //$NON-NLS-1$ props.put("project.id", prj.getId()); //$NON-NLS-1$ props.put("project.inceptionYear", prj.getInceptionYear()); //$NON-NLS-1$ props.put("project.name", prj.getName()); //$NON-NLS-1$ props.put("project.version", prj.getVersion()); //$NON-NLS-1$ props.put("project.url", prj.getUrl()); //$NON-NLS-1$ props.put("project.encoding", this.encoding); //$NON-NLS-1$ return props; }
From source file:io.sundr.maven.GenerateBomMojo.java
License:Apache License
/** * Returns the model of the {@link org.apache.maven.project.MavenProject} to generate. * This is a trimmed down version and contains just the stuff that need to go into the bom. * * @param project The source {@link org.apache.maven.project.MavenProject}. * @param config The {@link io.sundr.maven.BomConfig}. * @return The build {@link org.apache.maven.project.MavenProject}. *//*www .j a v a 2 s .c o m*/ private static MavenProject toGenerate(MavenProject project, BomConfig config, Collection<Dependency> dependencies, Set<Artifact> plugins) { MavenProject toGenerate = project.clone(); toGenerate.setGroupId(project.getGroupId()); toGenerate.setArtifactId(config.getArtifactId()); toGenerate.setVersion(project.getVersion()); toGenerate.setPackaging("pom"); toGenerate.setName(config.getName()); toGenerate.setDescription(config.getDescription()); toGenerate.setUrl(project.getUrl()); toGenerate.setLicenses(project.getLicenses()); toGenerate.setScm(project.getScm()); toGenerate.setDevelopers(project.getDevelopers()); toGenerate.getModel().setDependencyManagement(new DependencyManagement()); for (Dependency dependency : dependencies) { toGenerate.getDependencyManagement().addDependency(dependency); } toGenerate.getModel().setBuild(new Build()); if (!plugins.isEmpty()) { toGenerate.getModel().setBuild(new Build()); toGenerate.getModel().getBuild().setPluginManagement(new PluginManagement()); for (Artifact artifact : plugins) { toGenerate.getPluginManagement().addPlugin(toPlugin(artifact)); } } return toGenerate; }
From source file:io.sundr.maven.GenerateBomMojo.java
License:Apache License
/** * Returns the generated {@link org.apache.maven.project.MavenProject} to build. * This version of the project contains all the stuff needed for building (parents, profiles, properties etc). * * @param project The source {@link org.apache.maven.project.MavenProject}. * @param config The {@link io.sundr.maven.BomConfig}. * @return The build {@link org.apache.maven.project.MavenProject}. *//* w ww . ja va 2s . c o m*/ private static MavenProject toBuild(MavenProject project, BomConfig config) { File outputDir = new File(project.getBuild().getOutputDirectory()); File bomDir = new File(outputDir, config.getArtifactId()); File generatedBom = new File(bomDir, BOM_NAME); MavenProject toBuild = project.clone(); //we want to avoid recursive "generate-bom". toBuild.setExecutionRoot(false); toBuild.setFile(generatedBom); toBuild.getModel().setPomFile(generatedBom); toBuild.setModelVersion(project.getModelVersion()); toBuild.setArtifact(new DefaultArtifact(project.getGroupId(), config.getArtifactId(), project.getVersion(), project.getArtifact().getScope(), project.getArtifact().getType(), project.getArtifact().getClassifier(), project.getArtifact().getArtifactHandler())); toBuild.setParent(project.getParent()); toBuild.getModel().setParent(project.getModel().getParent()); toBuild.setGroupId(project.getGroupId()); toBuild.setArtifactId(config.getArtifactId()); toBuild.setVersion(project.getVersion()); toBuild.setPackaging("pom"); toBuild.setName(config.getName()); toBuild.setDescription(config.getDescription()); toBuild.setUrl(project.getUrl()); toBuild.setLicenses(project.getLicenses()); toBuild.setScm(project.getScm()); toBuild.setDevelopers(project.getDevelopers()); toBuild.setDistributionManagement(project.getDistributionManagement()); toBuild.getModel().setProfiles(project.getModel().getProfiles()); //We want to avoid having the generated stuff wiped. toBuild.getProperties().put("clean.skip", "true"); toBuild.getModel().getBuild().setDirectory(bomDir.getAbsolutePath()); toBuild.getModel().getBuild().setOutputDirectory(new File(bomDir, "target").getAbsolutePath()); for (String key : config.getProperties().stringPropertyNames()) { toBuild.getProperties().put(key, config.getProperties().getProperty(key)); } return toBuild; }
From source file:io.takari.maven.plugins.jar.Jar.java
License:Open Source License
private Iterable<Entry> jarManifestSource(MavenProject project) throws IOException { Manifest manifest = new Manifest(); Attributes main = manifest.getMainAttributes(); main.putValue("Manifest-Version", "1.0"); main.putValue("Archiver-Version", "Provisio Archiver"); main.putValue("Created-By", "Takari Inc."); main.putValue("Built-By", System.getProperty("user.name")); main.putValue("Build-Jdk", System.getProperty("java.version")); main.putValue("Specification-Title", project.getArtifactId()); main.putValue("Specification-Version", project.getVersion()); main.putValue("Implementation-Title", project.getArtifactId()); main.putValue("Implementation-Version", project.getVersion()); main.putValue("Implementation-Vendor-Id", project.getGroupId()); File manifestFile = new File(project.getBuild().getDirectory(), "MANIFEST.MF"); if (!manifestFile.getParentFile().exists()) { manifestFile.getParentFile().mkdirs(); }/*from w w w .j a v a 2 s . co m*/ ByteArrayOutputStream buf = new ByteArrayOutputStream(); manifest.write(buf); return singleton((Entry) new BytesEntry(MANIFEST_PATH, buf.toByteArray())); }
From source file:io.takari.maven.plugins.jar.Jar.java
License:Open Source License
protected Entry pomPropertiesSource(MavenProject project) throws IOException { String entryName = String.format("META-INF/maven/%s/%s/pom.properties", project.getGroupId(), project.getArtifactId());/*from w w w.j a va 2s . co m*/ ByteArrayOutputStream buf = new ByteArrayOutputStream(); Properties properties = new Properties(); properties.setProperty("groupId", project.getGroupId()); properties.setProperty("artifactId", project.getArtifactId()); properties.setProperty("version", project.getVersion()); PropertiesWriter.write(properties, null, buf); return new BytesEntry(entryName, buf.toByteArray()); }