I have a Maven archetype project and want to make some further things configurable. I tried:
Putting ${servicelocatorhost}$ and ${servicelocatorport} into my .tomcatplugin file.
Adding .tomcatplugin to the filtered files in the archetype metadata file.
Adding
<requiredProperties>
<requiredProperty key="servicelocatorhost">
<defaultValue>localhost</defaultValue>
</requiredProperty>
<requiredProperty key="servicelocatorport">
<defaultValue>2809</defaultValue>
</requiredProperty>
</requiredProperties>
to the archetype metadata file. When I build this, I get
Archetype IT 'basic' failed: Missing required properties in archetype.properties: servicelocatorhost, servicelocatorport
Until now I thought that the archetype.properties file is only for create-from-project but not building archetypes. Should I double the information for the properties and default values in such a file?
What exactly do I need to make this work?
When creating the archetype, there's also an integration test created for it. The values in the archetype.properties file are used when running the integration test (using the maven-invoker-plugin).
As an example: I have a simple archetype that filters a POM. In addition to GAV coordinates, I wanted the archetype user to provide the name of their Subversion repository and the application root directory so I could populate the <scm> block correctly. So I created variables ${scmRepo}, ${applicationRootDir}, and ${artifactId} in the archetype-metadata.xml file:
<requiredProperties>
<requiredProperty key="svnRepo" />
<requiredProperty key="applicationRootDir" />
<requiredProperty key="artifactId">
....
</requiredProperties>
then used them in the POM (src/main/resouces/archetype-resources/pom.xml):
<scm>
<connection>scm:svn:https://host.company.com/svn/${svnRepo}/trunk/${applicationRootDir}/${artifactId}</connection>
</scm>
The last step is to provide default values in the archetype.properties file. This is only used when you as the developer build the archetype, not when your community uses the archetype to create a project. It allows you to ensure the archetype does what you want. So in my example the archetype.properties file contains
svnRepo=maven
applicationRootDir=basic
artifactId=test
In the same directory, I have a verify.groovy file containing code to verify that the archetype worked correctly:
def xmlFile = new File(basedir, "pomFileDir/pom.xml")
assert xmlFile.exists()
def project = new XmlSlurper().parse(xmlFile)
....
assert "scm:svn:https://host.company.com/svn/maven/trunk/basic/test" == project.scm.connection.text()
I learned this from this blog post, this archetype documentation, and my own knowledge of using the Maven invoker plugin.
Related
I'm trying to create an archetype that I'll use for multiple services. The archetype generates a project just fine, all modules and files that I want included. Problem is, I'd like to set custom names for the modules and folders that the project has, depending on the service I'll be creating
Currently I have something like this (just an example):
<requiredProperties>
<requiredProperty key="testName"/>
</requiredProperties>
<module id="service-data" dir="service-data" name="service-data" />
What I'd like to happen is something like this:
<requiredProperties>
<requiredProperty key="testName"/>
</requiredProperties>
<module id="${testName}-data" dir="${testName}-data" name="${testName}-data" />
Basically, I'd like to give the archetype a parameter so that when I create a project using it, instead of service-data being my module name everytime, it ends up being say test-project (if testName = test).
I tried using the testName parameter as shown in this example, but what ends up happening is the module will literally be named ${testName}-data.
Using the requiredProperty works just fine for other pom.xml or other files in the project, but I'm really interested if I can set custom properties that will work directly in the archetype-metadata.xml file.
Thanks!
in my maven project, I've got a xml file in resources. Depending on some input parameter I want the file to be adapted before packaged into a jar or war. Of course, the original file shall not be touched.
It is not an option to create multiple xml-files and select a suitable one, for example, with spring profiles as there can be numerous combinations of contents in the xml file.
So, I thought of creating a maven plugin, that manipulates the file before packaging. Probably, I need to manipulate the file, when maven has copied the file to the target folder but before maven packages the file into the jar/war.
#Mojo(name = "manipulate-xml", defaultPhase = LifecyclePhase.PREPARE_PACKAGE)
public class MyMojo extends AbstractMojo {
#Parameter(defaultValue = "${project}", required = true, readonly = true)
MavenProject project;
#Parameter(property = "option")
String option;
public void execute() throws MojoExecutionException {
if (option.equals("optionA")) {
// get file from target and manipulate
} else if (option.equals("optionB")) {
// get file from target and manipulate
}
}
}
Then, I could embedded the maven plugin into my project and build the project with
mvn clean package -Doption=optionA
However, now I am stuck. I do not know, how to get the file from target and even if this is the right approach.
Besides, is it possible during the packaging to prevent some dependencies from being packaged into the jar/war?
I appreciate any help.
Depending on what manipulating means, you can use the possibilities of the maven resources plugin (https://maven.apache.org/plugins/maven-resources-plugin/index.html).
If you need to modify some simple values inside the xml, use properties in the xml and let the resources plugin replace them during build. The values for the build can be either in the pom.xml or given to maven via -Dproperty=value.
If you want to select a different files, define multiple maven profiles, in each you can configure the resources plugin to copy only the wanted files and then select the correct profile in the build.
If the built-in possibilities are not enough, you might even program your own filter for the resources plugin, that might be easier than writing a custom full fledged maven plugin.
I am using the Depgraph Maven Plugin and I'd like to ask if there's a way to limit the generated graph file (dot or gml) with those with a specific groupId e.g. com.mycompany.* pattern where only the dependencies within this package would be part of the graph.
I have tried both depgraph:aggregate and depgraph:aggregate-by-groupid but both results contains all the dependencies only organized into groupId
Which generated this:
According to the README of the depgraph plugin (bottom of the page), you can use the includes/excludes parameters similar to Maven's dependency plugin. The plugin also provides more information on this in their filtering wiki page.
Parameters in Maven are provided with -D, or in the configuration of your plugin. In case of command line you would use:
mvn depgraph:graph -Dincludes=com.mycompany*
I am running Jenkins ver. 2.60.2 and it doesn't seem possible, within a Maven Job, to define a local repository not in /usr/share/tomcat7/.m2.
Here are my attempts:
I created a Global Maven settings.xml and a Settings file with the Config File Management Plugin, that contains:
<settings>
<localRepository>/srv/maven/.m2/repository</localRepository>
...
</settings>
I Created a new Maven Project. Tried to make the Job see that file by attempting all of the following:
a) Defining either Settings file or Global settings file (I created two identical files) within the build step:
b) Adding a Pre-step Provide Configuration files, and then using the variable MY_SETTINGS either in the Goals and options or MAVEN_OPTS:
c) Use the Provide Configuration files within the build environment (and using the MY_SETINGS in the same way as in the previous step.:
However, none of these seems to work. The job always fails, trying to use the default maven repository location (/usr/share(tomcat7/.m2) - which I have no idea how to re-define:
provisioning config files...
copy managed file [MYFILE settings] to file:/srv/webapps/jenkins/jobs/testJob/workspace#tmp/config3408982272576109420tmp
provisioning config files...
copy managed file [MYFILE settings] to file:/srv/webapps/jenkins/jobs/testJob/workspace#tmp/config2203063037747373567tmp
Parsing POMs
using global settings config with name MYFILE settings
Replacing all maven server entries not found in credentials list is true
Deleting 1 temporary files
ERROR: Failed to create /usr/share/tomcat7/.m2
Finished: FAILURE
Do you know how to make this work within a Maven Job type in Jenkins?
I'm currently writing a custom maven plugin for generating a XML file in a multi-module maven project.
My maven structure is pretty standard: one parent project and a module by project components in the parent project folder:
-- Parent
-- module A
-- module B
-- module C
I need to list, by module, a set of classes flagged by a custom annotation.
I already wrote a set of custom annotations and an annocation processor to create a XML file at compile time in the corresponding module output directory (${project.build.outputDirectory}) .
Now i need to merge each module XML into one file, but i don't know how to access each modules from within my maven plugin except having each path set as parameters (i don't like this method).
Any idea on how to do this ?
Does maven plugins can traverse project modules ?
Thank you in advance.
To get the list list of all projects you can use:
List<MavenProject> projectList = MavenSession.getProjectDependencyGraph().getSortedProjects()
If one of your goals is correctly executed you will get everything you need. Every MavenProject contains a getBaseDir() etc.
After some researches, it seems that MavenProject.getCollectedProjects() will return the list of projects beeing manipulated by a goal execution in a multi-module project.