Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Maven for deployment

Tags:

I have this task for the project with 4 nested subprojects using Maven:

  1. For each child: jar-up resource directory including project dependencies
  2. Move up to the parent project
  3. With a single command extract all created archives into various remote destinations (full install), that may include http server, app server, file server, etc. (mostly *NIX). Destination is provided on subproject level
  4. It should also be possible to unzip/copy from the individual subproject (partial install)

Files are not Java - mostly various scripts and HTML

I'm looking at the various plugins to help with the task: assembly, dependency, antrun, unzip. Dependency looks promising but I need to unzip not only dependency jars but the (sub)project content as well. Also since I can't really tight the operation to the Maven lifecycle how would I trigger remote install? mvn dependency:unpack? That's not very descriptive or intuitive. Is is possible to create a custom goal (e.g. project:install) without writing a plugin?

Using Maven is company standard so please do not offer alternatives - I'm pretty much stuck with what I have

like image 438
Bostone Avatar asked Sep 10 '09 15:09

Bostone


People also ask

Can Maven be used for deployment?

The deploy plugin is primarily used during the deploy phase, to add your artifact(s) to a remote repository for sharing with other developers and projects. This is usually done in an integration or release environment.

What is Maven deployment?

The maven deploy plugin can be used to deploy either files or projects to the remote repository for sharing it with other developers and projects. There are various methods can be used to deploy your artifact to the remote repository by using a maven deploy plugin.

How do I run Maven deploy?

If you want to execute a specific execution from the command line, you'll need mvn deploy:deploy-file@deploy-file , i.e. @ the id of the configured execution, and make sure you're using at least Maven 3.3.

What does Maven deploy command do?

mvn deploy will put your packaged maven project into a remote repository for sharing with other developers.


2 Answers

Ok, I think the following might do what you need. The drawback of this approach is that there will be an interval between each deployment as the subsequent build is executed. Is this acceptable?

Define a profile in each project with the same name (say "publish"). Within that profile you can define a configuration to use the antrun-plugin to deliver the files with FTP (see below).

In the parent project you'll have a modules element, defining each project as a module. If you run mvn install -P publish, each project will be built in turn with the publish profile enabled, and the final artifact published to the target during the install phase. If you need to deploy additional files, modify the include element accordingly.

Note the parameters for the FTP task have been set as properties, this allows them to be overridden from the command-line and/or inherited from the parent POM.

<profiles>   <profile>     <id>publish</id>     <plugin>       <groupId>org.apache.maven.plugins</groupId>       <artifactId>maven-antrun-plugin</artifactId>       <executions>         <execution>           <id>ftp</id>           <phase>install</phase>           <configuration>             <tasks>               <ftp action="send"                    server="${ftp.host}" remotedir="${ftp.remotedir}"                    userid="${ftp.userid}" password="${ftp.password}"                    depends="${ftp.depends}" verbose="${ftp.verbose}">                 <fileset dir="${project.build.directory}">                   <include                      name="${project.build.finalName}.${project.packaging}"/>                 </fileset>               </ftp>             </tasks>           </configuration>           <goals>             <goal>run</goal>           </goals>         </execution>       </executions>       <dependencies>         <dependency>           <groupId>commons-net</groupId>           <artifactId>commons-net</artifactId>           <version>1.4.1</version>         </dependency>         <dependency>           <groupId>ant</groupId>           <artifactId>ant-commons-net</artifactId>           <version>1.6.5</version>         </dependency>         <dependency>           <groupId>ant</groupId>           <artifactId>ant-nodeps</artifactId>           <version>1.6.5</version>         </dependency>       </dependencies>     </plugin>     <properties>       <ftp.host>hostname</ftp.host>       <ftp.remotedir>/opt/path/to/install</ftp.remotedir>       <ftp.userid>user</ftp.userid>       <ftp.password>mypassword</ftp.password>       <ftp.depends>yes</ftp.depends>       <ftp.verbose>no</ftp.verbose>               </properties>   </profile> </profiles> 

Update: based on your comment: You could use the dependency plugin to download each dependency, except that a parent can't have a dependency on a child, and it will be built before the child. It would have to be another project. you also need to have somewhere the information for where to deploy them to. At the moment you have the target information in the individual projects so it isn't accessible in the deployer project.

Taking this approach, you can define multiple profiles in the new project, one for each artifact. Each profile defines a dependency:copy execution to obtain the jar and an antrun execution for one of the projects. Common configuration (such as the dependencies for the antrun plugin) can be pulled out of the profiles. Also be aware that the properties will be merged if you define multiple profiles, so yo may need to qualify them with the artifact name, for example ftp.artifact1.host.

<profiles>   <profile>     <id>deploy-artifact1</id>     <plugin>       <groupId>org.apache.maven.plugins</groupId>       <artifactId>maven-dependency-plugin</artifactId>       <executions>         <execution>           <id>copy-dependency</id>           <phase>prepare-package</phase>           <goals>             <goal>copy</goal>           </goals>           <configuration>             <artifactItems>               <artifactItem>                 <groupId>name.seller.rich</groupId>                 <artifactId>artifact1</artifactId>                 <version>1.0.0</version>                 <type>jar</type>                 <overWrite>false</overWrite>               </artifactItem>             </artifactItems>             <outputDirectory>${project.build.directory}/deploy-staging</outputDirectory>             <overWriteReleases>false</overWriteReleases>           </configuration>         </execution>       </executions>     </plugin>     <plugin>       <groupId>org.apache.maven.plugins</groupId>       <artifactId>maven-antrun-plugin</artifactId>       <executions>         <execution>           <id>ftp</id>           <phase>install</phase>           <configuration>             <tasks>               <ftp action="send"                    server="${ftp.host}" remotedir="${ftp.remotedir}"                    userid="${ftp.userid}" password="${ftp.password}"                    depends="${ftp.depends}" verbose="${ftp.verbose}">                 <fileset dir="${project.build.directory} includes="deploy-staging/"/>               </ftp>             </tasks>           </configuration>           <goals>             <goal>run</goal>           </goals>         </execution>       </executions>     </plugin>     <properties>       <!--if the properties differ between targets, qualify them with the artifact name-->       <ftp.host>hostname</ftp.host>       <ftp.remotedir>/opt/path/to/install</ftp.remotedir>       <ftp.userid>user</ftp.userid>       <ftp.password>mypassword</ftp.password>       <ftp.depends>yes</ftp.depends>       <ftp.verbose>no</ftp.verbose>               </properties>   </profile> </profiles>   
like image 74
Rich Seller Avatar answered Sep 29 '22 05:09

Rich Seller


Below POM will help to copy jar's file from project build directory to remote SFTP/FTP server.

  1. Use command mvn install -Dftp.password=password

Since I want to pass password from command prompt for security reason, I have used -Dftp.password=password After execution of above command all the jar files from maven project target folder will be deployed in MAVEN folder on server.com

    <plugin>          <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-antrun-plugin</artifactId>      <executions>         <execution>             <id>ftp</id>             <phase>install</phase>             <configuration>                 <tasks>                     <scp todir="[email protected]:/MAVEN/"                         sftp="true" port="22" trust="true" password="${ftp.password}"                         failonerror="false" verbose="true" passphrase="">                         <fileset dir="${project.build.directory}">                             <include name="*.jar" />                         </fileset>                     </scp>                 </tasks>             </configuration>             <goals>                 <goal>run</goal>             </goals>         </execution>     </executions>     <dependencies>         <dependency>             <groupId>org.apache.ant</groupId>             <artifactId>ant-jsch</artifactId>             <version>1.9.4</version>         </dependency>     </dependencies>  </plugin> 
like image 37
Vinayak Dornala Avatar answered Sep 29 '22 06:09

Vinayak Dornala