I am trying to build a groovy project using maven. My packaging type is war file. Maven is building the project and putting all dependent libraries in WEB-INF/lib folder but it is compiling all code into class files and putting it into WEB-INF/classes folder. Is there a way I can tell maven to build jar file for my project also and put it into WEB-INF/lib folder.
My pom.xml looks like this :
<groupId>com.myproject</groupId>
<artifactId>ExampleProject</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>My Example Project</name>
<url>http://maven.apache.org</url>
<dependencies>
...
...
...
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/groovy</directory>
<excludes>
<exclude>**/*.groovy</exclude>
</excludes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/groovy</source>
<source>src/main/resources</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/groovy</source>
<source>src/test/resources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>master</finalName>
</build>
In these scenarios the usual approach is to you separate your library code in a different module that will be a dependency from your war module. For this suggestion you can see also how to generate jar and war both in project .
However if you still prefer to go with the solution you mention, you can do it with the following configuration in your pom
<configuration>
..
<attachClasses>true</attachClasses>
<archiveClasses>true</archiveClasses>
</configuration>
(see http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html, and how to use class file from another war)
Related
I have found a package I want to use, it's using Maven, but it's sources are split between two directories, one of them contains auto-generated stuff, the other, .xtend source files.
The problem is, these directories "can't see each other" when compiling; this used to work given build reports from two years ago on the same project.
Conjecture: This broke because of a change between Maven 3.5 and 3.6, I guess it used to add all source directories to the classpath first, then compiled the files, but it doesn't anymore, now I see only the current one, although both are reported as source directories.
I don't want to restructure the project to fix these errors.
So given two directories, A and B, what's the proper way to add B to the classpath from A, and A from B, so that this works as it is?
I've reduced it to:
project/
|- src/
|- src-gen/
|- xtend-gen/
|- .classpath
|- build.properties
`- pom.xml
build.properties:
source.. = src/,\
src-gen/,\
xtend-gen/
bin.includes = META-INF/,\
.
additional.bundles = org.apache.commons.logging
pom.xml:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>com.example.lang.dsl</artifactId>
<packaging>eclipse-plugin</packaging>
<build>
<resources>
<resource>
<directory>${project.build.directory}/xtext</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>initialize</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/xtext</outputDirectory>
<resources>
<resource>
<directory>src</directory>
<includes>
<include>**/*.mwe2</include>
<include>**/*.xtext</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.fornax.toolsupport</groupId>
<artifactId>fornax-oaw-m2-plugin</artifactId>
<executions>
<execution>
<id>xtext</id>
<phase>generate-sources</phase>
<goals>
<goal>run-workflow</goal>
</goals>
<configuration>
<workflowEngine>mwe2</workflowEngine>
<workflowDescriptor>com.example.lang.dsl.GenerateComp</workflowDescriptor>
<timestampFileName>gen.timestamp</timestampFileName>
<jvmSettings>
<fork>true</fork>
<jvmArgs>
<jvmArg>-Xms128m</jvmArg>
<jvmArg>-Xmx512m</jvmArg>
<jvmArg>-XX:MaxPermSize=128m</jvmArg>
</jvmArgs>
</jvmSettings>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.xtend</groupId>
<artifactId>xtend-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<version>0.1</version>
</project>
Maven has resource plugin that can delimiter a character that can tell it in pom file , for example in the blow config define it that replace "#" character.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/filtered-resources/scripts</outputDirectory>
<resources>
<resource>
<directory>src/assemble/resources/scripts</directory>
<filtering>true</filtering>
</resource>
</resources>
<useDefaultDelimiters>false</useDefaultDelimiters>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
</configuration>
</execution>
</executions>
</plugin>
but it work on files that directly exists in my project , i want to do the same job in a jar file , i have a jar file that exists some java script plugins and i want add it to my project but i need replace somethings on it .
and the dependency is something like this :
<dependency>
<groupId>sample.javascript</groupId>
<artifactId>jsLibrary/artifactId>
<version>1.0.0</version>
</dependency>
1) you need to unpack your jar. You can use this plugin: https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-artifacts.html
2) change
<directory>src/assemble/resources/scripts</directory>
to the directory with unpacked jar
I'm trying to build a Maven parent project script which incorporates a 3rd party project (with all its dependencies) and another couple of projects of mine.
I'd like to copy all dependencies (I mean ALL dependencies, including jars requested by other dependencies which in turn are requested by one of my child project) to a parent project directory, e.g. "lib", so that I can just run java with a wildcard classpath:
java -cp "lib/*" package.blah.blah.Main
I tried with various methods using the maven-dependency-plugin such as copy-dependencies, but all I can point Maven to is my own child projects, not their dependencies (I mean in the parent project pom).
This is the parent project pom I've been writing:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<properties>
<server.version>7.0.2</server.version>
<ext.version>1.0-alpha3-SNAPSHOT</ext.version>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>org.kontalk</groupId>
<artifactId>tigase-kontalk</artifactId>
<version>devel</version>
<packaging>pom</packaging>
<modules>
<module>../tigase-server/modules/master</module>
<module>../tigase-extension</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-artifact</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.kontalk</groupId>
<artifactId>tigase-server</artifactId>
<version>${server.version}</version>
<type>jar</type>
</artifactItem>
<artifactItem>
<groupId>org.kontalk</groupId>
<artifactId>tigase-extension</artifactId>
<version>${ext.version}</version>
<type>jar</type>
</artifactItem>
</artifactItems>
<outputDirectory>bin</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.kontalk</groupId>
<artifactId>tigase-server</artifactId>
<version>${server.version}</version>
</dependency>
<dependency>
<groupId>org.kontalk</groupId>
<artifactId>tigase-extension</artifactId>
<version>${ext.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
It will copy just the two direct child project jars in the bin directory, but nothing else.
I'm thinking of doing a manual copy (as in a Maven copy) to brutally copy all jars from my child projects' target/dependency directories, but it just seems... brutal.
If it can't be done with the existing Maven software, I can even be content with using maven exec directly (which, I hope, should set up classpath automatically, right?)
EDIT: I'd like to modify the child projects poms as little as possible, especially the 3rd party one.
If you want to build a simply runnable jar file with maven then you can use this config:
<profiles>
<profile>
<id>dist</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>yourJarName</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>your.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>dist</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
then you can build it using mvn clean package -Pdist
edit: then you can run it with java -jar target/yourJarName.jar
I ended up using copy-dependencies. This is the main project pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/jars</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>jars</outputDirectory>
<resources>
<resource>
<directory>../tigase-extension/target/lib</directory>
<excludes>
<exclude>junit*</exclude>
</excludes>
</resource>
<resource>
<directory>../tigase-server/target/lib</directory>
<excludes>
<exclude>junit*</exclude>
</excludes>
</resource>
<resource>
<directory>../tigase-extension/target</directory>
<includes>
<include>tigase-extension-*.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
In the submodules I've added to the same plugin this code:
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
Basically, the submodules copy their dependency jars into target/lib, then the main project copies those jars into its own jars folder.
It has some project-specific filenames and other hard-coded things, but still I think this is the best option if I want to use only Maven-provided tools. Probably a new Maven plugin would have been optimal, but I don't really have the time.
I am using addjars-maven-plugin to add all the jars present in web-inf/lib to my classpath and i am able to build my web-application and package it as war.
Is there any similar way to add all the jars present in some folder to classpath, while building java classes and package it as jar?
I tried the same plugin as below. But, when i try to build my project, the jars are downloaded to my local repository. But still i get class not found exception.
Kindly help. Thanks in advance!
<build>
<plugins>
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.2</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/main/extlib</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Full pom.xml content:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>testlogger</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.2</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/main/extlib</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I could achieve adding the jars present in any folder by using addjars-maven-plugin as above but, the only change i had to make is change the version of the plugin from 1.0.2 to 1.0.5. Thanks all for the support!!
I had the same challenge with addjars-maven-plugin. Calling mvn clean install it builds successfully but under eclipse (RAD 9.5) my source files could not be compiled because of missing jars. Now add the maven-compiler-plugin:
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<includes>
<include>${some.dir}/libs/*.jar</include>
</includes>
</configuration>
</plugin>
Nevertheless those jars are not added under Maven Dependencies in the project structure.
I have a Java project entirely consisting of junit/integration tests which is managed by maven. One of the dependencies is a zip archive, the contents of which I would like to be available on the classpath when the tests are run. Since maven does not put the content of a zip dependency on the classpath I have had to come up with what I consider to be a hacky workaround. I unpack the zip archive to a temp directory then copy one of the resulting directories into the /test-classes folder. I also had to make the clean step delete the temp directory. Here are the relevant parts of the pom:
<groupId>com.my.package</groupId>
<artifactId>test-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>My Test Project</name>
<properties>
<config.artifactId>environment-dev</config.artifactId>
<config.version>2.0.8-SNAPSHOT</config.version>
<tempDir>${project.basedir}/temp</tempDir>
</properties>
<build>
<plugins>
...
<!-- clean out our custom temp directory as well as the default dir during clean phase-->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.3</version>
<configuration>
<filesets>
<fileset>
<directory>${tempDir}</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<!-- since the config dependency is a zip it does not get added to the classpath. So we extract
it to a temp dir, then copy the content we need into a directory on the classpath -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack-config</id>
<phase>compile</phase>
<goals><goal>unpack-dependencies</goal></goals>
<configuration>
<includeGroupIds>com.my.package.config</includeGroupIds>
<includeArtifactIds>${config.artifactId}</includeArtifactIds>
<includeClassifiers>config</includeClassifiers>
<outputDirectory>${tempDir}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- copy the content of the zip file that we extracted into a directory on the classpath -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>compile</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/test-classes/TargetDir</outputDirectory>
<resources>
<resource>
<directory>${tempDir}/${config.artifactId}-${config.version}/TargetDir</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
...
<dependency>
<groupId>com.my.package.config</groupId>
<artifactId>${config.artifactId}</artifactId>
<version>${config.version}</version>
<classifier>config</classifier>
<type>zip</type>
</dependency>
</dependencies>
There must be a better way of doing this.
Can I force maven to treat the zip file as if it were a jar? The link I provided has a tantalising hint that this might once have been possible, but I can't find anything relevant in the documentation. This seems like such a simple thing to be able to do, I really hope I've just missed a config parameter somewhere. Can anyone suggest a better way of getting the content of a zip dependency onto the classpath?
I would unzip the dependency into a subdirectory of the target directory and add that directory to the additionalClasspathElements configuration of the surefire plugin.
<properties>
<config.artifactId>environment-dev</config.artifactId>
<config.version>2.0.8-SNAPSHOT</config.version>
<unzipDir>${project.build.directory}/addTestClasspath</unzipDir>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack-config</id>
<phase>compile</phase>
<goals><goal>unpack-dependencies</goal></goals>
<configuration>
<includeGroupIds>com.my.package.config</includeGroupIds>
<includeArtifactIds>${config.artifactId}</includeArtifactIds>
<includeClassifiers>config</includeClassifiers>
<outputDirectory>${unzipDir}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>${unzipDir}</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
In this case you can omit the clean plugin config because everything is under the target folder which will be deleted by the clean plugin by default.
Sadly this configuration does only work on the command line and not within eclipse, because the m2e plugin does not honor the additionalClasspathElement. See the jira issue MNGECLIPSE-1213