I've read some questions here about how to set a property (most of them talked about the version number for an application) from a maven plugin.
It seems there's no easy way of doing this and the best solution I found is to have a filter.properties file which is updated from the plugin and used by the main pom file to filter the desired resources.
I tried another solution after I read this from the Maven documentation (Maven filter plugin):
Variables can be included in your resources. These variables, denoted
by the ${...} delimiters, can come from the system properties, your
project properties, from your filter resources and from the command
line.
I found interesting that variabled can be read from system properties. So, I modified my plugin to set a system property like this:
System.setProperty("currentVersion", appCurrentVersion);
However, filtered resources don't seem to read this value.
Could anybody tell me what's wrong with this approach?
UPDATE: I'm running my plugin in the validate phase.
Thanks a lot.
Don't set it as System Property, set it as Maven Project property
// inject the project
#Parameter(defaultValue = "${project}")
private org.apache.maven.project.MavenProject project;
// and in execute(), use it:
project.getProperties().setProperty("currentVersion", appCurrentVersion);
See:
Mojo Developer Cookbook
MavenProject javadoc
An edit suggested using Properties.put() instead of Properties.setProperty(). While technically, Properties implements Map, this usage is discouraged explicitly in the Properties javadoc.
Maven sets properties in initialize phase. I assume that in that phase maven loads system properties. And after that maven doesn't load system properties again. If you try to add a system property after this phase than it's not loaded.
Try to run your plugin in validate phase.
Related
I know how to get it if version is defined in application.properties, but how do I get it from build.gradle?
The general flow is:
Define a property in your application.properties that has placeholders, i.e. gradleVersion=${version}.
Configure Gradle's default task that copies your resource files out to the build directory (called processResources) to filter / expand those properties
Read in the gradleVersion property like any other Spring property
Note that it'll require you to invoke Gradle in order to properly resolve the gradleVersion property (as Gradle is the one putting the value in there). bootRun should already depend on processResources, so if you're using that you should be fine.
I have muli moduled maven based application. There are several change sets in each module. I want to write integration tests in one of them. I know it's bad idea but, i have business log constraints.
In considered module i have code that uses repositories from 2 others modules. During tests i have to initialize tables from change sets in different modules. I don't know how to use absolute path to change sets or how to use liquibase config.
I tried use include or includeAll tags, but in classpath where tests run i can't call change sets out of module.
Hope for your ideas.
you should be able to create liquibase instance in your junit by passing absolute file paths. You can refer liquibase tests in github for more info
new Liquibase(changeLogFile, FileSystemResourceAccesor, database);
https://github.com/liquibase/liquibase/blob/master/liquibase-integration-tests/src/test/java/liquibase/dbtest/AbstractIntegrationTest.java#L890
I want to nicely recognize situation when maven plugin is executed from maven project or not, because I need different default parameters when execution is outside of maven project.
I can inject #Component MavenProject project into plugin Mojo, but this is setted to test:test:jar:1 when there is no pom.xml.
I can inject base dir #Parameter(defaultValue = "${basedir}") File baseDir and check for pom.xml file, but this smells (with polyglot maven there is no pom.xml anymore).
How to check if project is executed inside or outside maven project?
I have specific goal and I want to use this same goal for both situation.
I feel the question is not answered enough. My proposal is:
#Component
private MavenSession mavenSession;
boolean insideMavenProject = mavenSession.getRequest().isProjectPresent()
The important thing is that you clearly make a decision which goal should be used from CLI only and which should be used within the pom file... The basic decision can be made by using the following:
#Mojo( name = "xxxx", requiresProject = true,... )
so the requiresProject means you need to have a pom.xml which means no calling via CLI.
If you omit this you can give the opportunity to use a goal from CLI. So best is to use one goal which is intended for calling from CLI and an other goal which is intended to be used from the pom.xml ..
Furthermore to inject a MavenProject you should do this like this:
#Parameter( defaultValue = "${project}", required=true, readonly=true)
private MavenProject project;
cause a MavenProject is not a #Component. Apart from that i don't understand your information about test:test:jar:1 always...and what i'm interested in is what kind of plugin are you trying to write?
Is it possible to do this?
For example could one do something like:
System.getEnv("$(env.BUILD_URL)");
I do not have access to Jenkins, so can't try it out myself :(
Any help would be greatly appreciated.
Assuming you're using Maven (you've tagged this question as such), I simply add the following to my pom.xml file:
...
<properties>
<!-- Hudson properties: see http://wiki.hudson-ci.org/display/HUDSON/Building+a+software+project#Buildingasoftwareproject-HudsonSetEnvironmentVariables -->
<jenkins.buildId>${env.BUILD_ID}</jenkins.buildId>
<jenkins.buildNumber>${env.BUILD_NUMBER}</jenkins.buildNumber>
<jenkins.buildTag>${env.BUILD_TAG}</jenkins.buildTag>
<jenkins.cvsBranch>${env.CVS_BRANCH}</jenkins.cvsBranch>
<jenkins.executorNumber>${env.EXECUTOR_NUMBER}</jenkins.executorNumber>
<jenkins.hudsonUrl>${env.HUDSON_URL}</jenkins.hudsonUrl>
<jenkins.javaHome>${env.JAVA_HOME}</jenkins.javaHome>
<jenkins.jobName>${env.JOB_NAME}</jenkins.jobName>
<jenkins.svnRevision>${env.SVN_REVISION}</jenkins.svnRevision>
<jenkins.workspace>${env.WORKSPACE}</jenkins.workspace>
</properties>
...
...and then from your code you can simply do a:
String url = System.getProperty("jenkins.hudsonUrl"); // could be null
Putting these into properties makes life more simple for my purposes, especially when using Maven profiles to control my builds. For example, I make sure to create a "jenkins" profile that is activated when I build on a Jenkins build server. When this is done, all the aforementioned jenkins properties are set. When not run as a jenkins profile, those properties are set to some other default value. Anyway, that's another topic, but food for thought. Hopefully it make sense.
I'm using maven to build a ".ear" project that resolves dependencies from a maven repository, and then packages them into an ear (that's probably a redundant sentence...).
When the dependencies show up in the ear file, they're named according to this format:
<artifactId>-<version>.<type>
I'd like them to be named:
<artifactId>.<type>
Can someone point me in the right direction?
If you're using the maven-assembly-plugin to build your ear, you can use the outputFileNameMapping property in your descriptor: http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet
However, you're probably better off using the maven-ear-plugin, in which case you can customize the bundleFileName, as described here.
Set the finalName property. See http://maven.apache.org/plugins/maven-assembly-plugin/assembly-mojo.html for more details