The "install" goal copies the artifact to the target directory and to the local repository.
How can I tell Maven to copy it also to a given directory (like the deploy directory of JBoss for example).
The goal copy of maven-dependency-plugin does what you want, see the example.
It is however not a good idea to copy anything outside your target directory (or ${project.build.directory} to be precise) - especially if such action is attached to a build phase, because it introduces unexpected side-effects of the build, and sometimes even loss of reproducibility.
As #Andreas_D notes, there is a better alternative for JBoss deployment purpose; similarly for deploying to other appservers.
According to http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html you can copy the just built artifact to a specific directory:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-installed</id>
<phase>install</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
</artifactItem>
</artifactItems>
<outputDirectory>some-other-place</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
If you want to copy file to a webserver (local or distant) you can use Maven upload plugin :
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-upload-plugin</artifactId>
<version>1.1</version>
<configuration>
<resourceSrc>
${project.build.directory}/${project.build.finalName}.${project.packaging}
</resourceSrc>
<resourceDest>${jboss.deployDir}</resourceDest>
<serverId>${jboss.host}</serverId>
<url>${jboss.deployUrl}</url>
</configuration>
</plugin>
And to configure parameters in a smart way, I use maven profiles :
<profiles>
<!-- local deployment -->
<profile>
<id>developpement</id>
<properties>
<jboss.host>localhost</jboss.host>
<jboss.deployDir>appli/jboss-4.0.4.GA/server/default/deploy/</jboss.deployDir>
<jboss.deployUrl>file://C:/</jboss.deployUrl>
</properties>
</profile>
<!-- distant deployment -->
<profile>
<id>validation</id>
<properties>
<jboss.host>ENV_val</jboss.host>
<jboss.deployDir>/home/envval/jboss/server/default/deploy/</jboss.deployDir>
<jboss.deployUrl>scp://PROJECT_LAN_HOST</jboss.deployUrl>
</properties>
</profile>
</profiles>
I've created an "ant launcher", to use it by clicking under Eclipse ant view :
<target name="copy war to JBoss local" description="Copy war to local JBoss">
<maven goal="upload:upload" options="-Pdeveloppement" />
</target>
But you can simply run it on a command line :
mvn upload:upload -Pdeveloppement
By the way, for distant deployment, you may need a login password for scp to work. You have to add them to you Maven settings.xml file :
<settings>
...
<servers>
<server>
<id>ENV_val</id>
<username>login</username>
<password>password</password>
</server>
</servers>
...
</settings>
The best approach would be to use a plugin which will actually deploy your application, such as cargo or jboss-maven plugin (credit to #Andreas_D for that one).
This would be a better approach to using a copy or generic upload tool since deploying is what you are actually trying to do.
With the cargo plugin you have the option to deploy to a variety of running servers. We took this approach to test locally in jetty using the jetty plugin during the build and had a profile to deploy to tomcat on demand via cargo.
Note: If you have your target server (JBOSS) installed locally on the dev box as well then you can also use cargo to start/stop your server during your build process as well. The downside of this approach is that you will need it to reference it's location in the pom file, so either all devs install it in the same location or a system property that defines where it is located (similar to JAVA_HOME).
Related
I have a project that consist of 3 different libraries. When I run install script it takes all libraries from repo and run mvn clean install on them. But this version of library already installed in repo. Is there a way to skip install phase if version in pom.xml equal version in my local repo.
I know that I can use local repo and just set dependencies. But my boss want that our project can build only with public repos and without any our repos.
You can bypass like this
-Dmaven.install.skip=true
<profiles>
<profile>
<id>skipInstall</id>
<activation>
<property>
<name>maven.install.skip</name>
<value>true</value>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>default-install</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
Last week Olivier Lamy patched this jira.
MINSTALL-73
Most maven plugins can be skipped by specifying something like:
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>X.Y</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
you can also set up build profiles to set properties and use that to determine the value. for example, running the command: mvn -Pexample would select the "example" profile. The POM would then contain:
...
<properties>
<skip.install>false</skip.install>
...
</properties>
...
<profile>
<id>example</id>
<properties>
<skip.install>false</skip.install>
</properties>
</profile>
...
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>X.Y</version>
<configuration>
<skip>${skip.install}</skip>
</configuration>
</plugin>
...
Using these POM additions, the default behavior for the install plugin will be to perform its default goal, but if the example profile is selected, then the install plugin will skip its goal.
Using what I learned from the other answers, this was the cleanest result for me.
In my super pom I added a pluginManagement/plugin to disable default-install and default-test phases when the property deployOnly is set.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>default-install</id>
<configuration>
<skip>${deployOnly}</skip>
</configuration>
</execution>
<execution>
<id>default-test</id>
<configuration>
<skip>${deployOnly}</skip>
</configuration>
</execution>
</executions>
</plugin>
So on the command line, I can disable install and test phases by adding -DdeployOnly.
mvn clean install #build and test everything
mvn deploy -DdeployOnly #just deploy it
I know that I can use local repo and just set dependencies. But my boss want that our project can build only with public repos and without any our repos.
Are you sure you understood correctly what you boss meant? I interpret the above as "don't install third party libraries in your local repository, use only libraries available in public repositories". This is different from "don't use your local repository" which is basically impossible, that's just not how maven works. I'd try to clarify this point.
Apart from that, I don't get the question which is very confusing (what repo are you talking about? What is the install script doing? Why do you call clean install on libraries? etc).
Extending the other answers, from the future.
Maven plugins have a surprisingly high freedom, how do they run. If they want, they can ignore/override the typical pom.xml settings. Furthermore, also the <configuration><skip>true</skip></configuration> is only a convention, nothing obligates a plugin to follow it, except that most of them is developed so.
My experiments with the recent problem show, that both #Cemo's and #MiloshBoroyevich solution should be utilized, also the plugin requires both to really let us in peace. More concretely, the only working configuration by me was this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>default-install</id>
<phase>none</phase>
</execution>
</executions>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
One of your options is to put the deployment to another module. I.e. have one pom.xml build the artifact and install it to the local repo, and another pom.xml to deploy it. This separation is quite common in larger projects, where the testsuite is sometimes a separate module or even a project, the packaging happens in several stages, etc.
- pom.xml - myProject-root - type=pom
- pom.xml - myProject-artifact - type=jar
- pom.xml - myProject-deploy - type=pom, does the deployment, skips it's own `install` goal
Sonatype has a repository that I want to deploy a jar file to, and they ask for separate files for application, sources, and javadocs:
Example:
example-application-1.4.7.pom
example-application-1.4.7.jar
example-application-1.4.7-sources.jar
example-application-1.4.7-javadoc.jar
In Scala SBT, I have a command called "package" that generates the jar file for the project, but that only generates "example-application-1.4.7.jar".
Question: What should I do to generate the other two jar files?
In Maven, in order to get the additional -sources and -javadoc artifacts, add to your POM file the following:
<build>
<plugins>
<!-- additional plugin configurations, if any.. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note the snippet above:
We are invoking the Maven Source Plugin to create an additional jar files for sources
We are invoking the Maven Javadoc Plugin to create an additional jar files for javadoc
Executing
mvn clean package
You will find these two additional jars in the target folder.
The .pom file instead is generated during the install phase, but it is not placed under the target folder. Basically, it is a copy of your pom.xml file, with a different extension and used by Maven during the dependency mediation process to check which transitive dependencies are required by the concerned artifact.
Executing
mvn clean install
Maven will install the artifact in your local cache (in your machine), under path_to_cache/.m2/repository/your_groupId/your_artifactId/your_version/. In this folder, you will also find the .pom file, which normally you don't need to distribute (it is created automatically by Maven).
Further note: you probably don't want to generate these additional jar files at each and every build, so to speed up normal builds and have them only on demand, you could wrap the snippet above in a Maven profile.
You can achieve this by removing the snippet above from your build section and add a further section at the end of your pom:
<profiles>
<profile>
<id>prepare-distribution</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
So that normal builds would not create these jars anymore, but when executing the following:
mvn clean install -Pprepare-distribution
You would instead get them back. the -P option is actually activating on demand the profile defined with the id prepare-distribution.
With Maven 3 a default profile already comes as part of the super pom which perform exactly the same actions (sources and javadoc artifact), hence no need to add anything to your existing project. Simply run:
mvn clean install -Prelease-profile
Or, to activate it via a property
mvn clean install -DperformRelease=true
However, as also specified in the super pom, this profile may be removed in future releases (although there since first Maven 3 version till version 3.3.9 so far)
NOTE: The release profile will be removed from future versions of the super POM
The main reason behind this warning is most probably to push for the usage of the Maven Release Plugin, which indirectly makes use of this profile via the useReleaseProfile option of the release:perform goal.
As highlighted by comments, if you are not familiar with maven (especially via console) I would definitely recommend to
Go through the official Maven in 5 minutes documentation for a quick but worthy look.
Play with Maven from the command line, is there where Maven gives you its best. IDE integrations are great, but command line is the real turning point.
Then play with the POM customization above, to get familiar with some concepts and behaviors, first directly as part of your default build, then moved to a profile.
Then, and only then, move to the Maven Release Plugin usage. I recommend it as last step because you would already have acquired more confidence and understanding and see it as less magic and more reasonable approach.
I want to rename static files when building my WAR file using maven-war-plugin, with version number. For intance, in my project I have a file:
src/main/webapp/css/my.css
I want it to appear in the WAR file as:
css/my-versionNumber.css
The same question was already asked (see Rename static files on maven war build), but the accepted answer is to rename a directory. I do not want to rename a directory, I want to rename the actual file.
Is this possible?
OK, I found a way, the principles come from: Automatic update of generated css files via m2e.
Renaming files
I chose to use Maven AntRun Plugin, because it allows to rename several files using a replacement pattern, see https://stackoverflow.com/a/16092997/1768736 for details.
Alternative solutions were to use:
maven-assembly-plugin: but it doesn't support renaming of filesets, only renaming of individual files (see file assembly descriptor and comments of this answer: https://stackoverflow.com/a/4019057/1768736)
maven-resources-plugin: it allows to copy resources, not to rename them (see copy resources mojo and comments of this question: Renaming resources in Maven)
Make renamed files available for maven-war-plugin
Copy files: the idea is to copy the renamed files into a temp directory, to be added by maven-war-plugin to the WAR file as a web-resource. maven-war-plugin builds the WAR during the packaging phase, so we will need to copy renamed files before that.
Prevent maven-war-plugin to manage the files to be renamed by maven-antrun-plugin: this is done by using the parameter warSourceExcludes.
Make it work from within Eclipse with m2e-wtp
Change lifecycle mapping: the problem is that m2e by default doesn't execute all lifecycles defined in the POM file (to see the lifecycles executed/ignored, from Eclipse, go to your project properties, then Maven > Lifecycle Mapping). So you need to use the fake plugin org.eclipse.m2e.lifecycle-mapping to add the maven-antrun-plugin lifecycle, see life cycle mapping documentation.
Change maven-antrun-plugin output directory: the problem is that m2e-wtp acquires its web-resources before any lifecycle can be launched, so, before maven-antrun-plugin can rename the files. As a workaround, we create a profile, activated only when the project is built by m2e, to change the property used to set maven-antrun-plugin output directory, to write directly into m2e-wtp web-resources.
That's it! POM snippet:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<properties>
<!-- properties used to rename css files with version number. -->
<css.version>${project.version}</css.version>
<!-- Temp directory where to copy renamed files, later added by maven-war-plugin as web-resources. -->
<rename.tmp.directory>${project.build.directory}/rename_tmp</rename.tmp.directory>
</properties>
<!-- There is a problem when running the webapp from Eclipse: m2e-wtp acquires
the web-resources before any lifecycle can be launched, so, before
maven-antrun-plugin can rename the files. We define a profile so that
maven-antrun-plugin copies files directly into the m2e-wtp web-resources directory,
when running from Eclipse. -->
<profiles>
<profile>
<id>m2e</id>
<!-- This profile is only active when the property "m2e.version"
is set, which is the case when building in Eclipse with m2e,
see https://stackoverflow.com/a/21574285/1768736. -->
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<properties>
<rename.tmp.directory>${project.build.directory}/m2e-wtp/web-resources/</rename.tmp.directory>
</properties>
</profile>
</profiles>
...
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
<id>rename-resources</id>
<!-- perform copy before the package phase,
when maven-war-plugin builds the WAR file -->
<phase>process-resources</phase>
<configuration>
<target>
<!-- copy renamed files. -->
<copy todir="${rename.tmp.directory}/css/">
<fileset dir="src/main/webapp/css/">
<include name="**/*.css" />
</fileset>
<!-- See other Mappers available at http://ant.apache.org/manual/Types/mapper.html -->
<mapper type="glob" from="*.css" to="*-${css.version}.css"/>
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<!-- We do no let the maven-war-plugin take care of files that will be renamed.
Paths defined relative to warSourceDirectory (default is ${basedir}/src/main/webapp) -->
<warSourceExcludes>css/</warSourceExcludes>
<webResources>
<!-- include the resources renamed by maven-antrun-plugin,
at the root of the WAR file -->
<resource>
<directory>${rename.tmp.directory}</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
...
</plugins>
<!-- When running server from Eclipse, we need to tell m2e to execute
maven-antrun-plugin to rename files, by default it doesn't. We need to modify the life cycle mapping. -->
<pluginManagement>
<plugins>
<!-- This plugin is not a real one, it is only used by m2e to obtain
config information. This is why it needs to be put in the section
pluginManagement, otherwise Maven would try to download it. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<versionRange>[1.0.0,)</versionRange>
<goals>
<goal>run</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<!-- set to true, otherwise changes are not seen,
e.g., to a css file, and you would need to perform
a project update each time. -->
<runOnIncremental>true</runOnIncremental>
</execute >
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
...
</build>
...
</project>
You need define
<properties>
<css.version>1.1.0</css.version>
</properties>
After that call it by EL
css/${css.version}.css
I have multi-module maven project. I can see generated project dosumentation after mvn site, but it is in every module target folder.
how to get maven project site into one folder?
(I don't really need to deploy, I will put generated site manually into GitHub pages.)
Plugin docs:
http://maven.apache.org/plugins/maven-site-plugin/
References <distributionManagement> <site> section, but there is no trace how to make output into a defined folder.
Also tried mvn site:jar - it make .jar again in every module target.
I would recommend to use the site-maven-plugin following part to distribute generated sites to github:
<profile>
<id>github</id>
<build>
<plugins>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.8</version>
<configuration>
<message>Creating site for ${project.version}</message>
<server>github</server>
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>post-site</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
To preview your site before publishing you can use the stage goal of maven-site-plugin like this:
mvn site:stage -DstagingDirectory=C:\fullsite
Is there a generic way to do this for any maven plugin - run based on user preference/ disable it based on a property file?
Have a properly working maven plugin using com.mysema.querydsl, now want to change is so it only runs if a particular flag/ command line options is provided.
<plugin>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
<version>${querydsl-maven-plugin.version}</version>
//executions
<configuration>
<jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
<jdbcUrl>jdbc:mysql://myurlk:port/db</jdbcUrl>
<jdbcUser>id1</jdbcUser>
<jdbcPassword>ccc</jdbcPassword>
<packageName>com.sample</packageName>
<targetFolder>${project.basedir}/src/main/java</targetFolder>
<schemaPattern>APP</schemaPattern>
//goal prefix here?
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
</dependency>
</dependencies>
</plugin>
Tried to add
<executions>
<execution>
<id>execution1</id>
<phase>test1</phase>
<goals>
<goal>export</goal>
</goals>
</execution>
</executions>
and a goal prefix
<goalPrefix>mysema1</goalPrefix>
But not working. Want a way so this plugin is not run when we do a default
mvn clean install
But need to add another flag to make it run?
Using Apache Maven 3.0.4
Did you try to put the plugin execution into a Maven profile? There are several triggers to enable a profile for a build (e.g. OS, Java version, property value or the profile id itself on the command line).
See http://maven.apache.org/guides/introduction/introduction-to-profiles.html for more details.
Define a profile, add the plugin definition into the profile and add a property trigger for the profile like this:
<project>
...
<profiles>
<profile>
<id>profile-id</id>
<activation>
<property>
<name>myProperty</name>
</property>
</activation>
<build>
<plugins>
<plugin>
...
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
When you build yout project with mvn clean install the plugin will not be executed, when you build your project with mvn clean install -DmyProperty or mvn clean install -Pprofile-id your plugin will be executed. In the second case the property activation trigger for the profile is obsolete.