I have put up a maven project on git repository which is using maven-war-plugin to create war directly in tomcat directory. But on different systems, tomcat directory path can be different.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<outputDirectory><file-path></outputDirectory>
</configuration>
</plugin>
I want this outputDirectory parameter to be configurable without needing to change pom.xml on local systems.
I faced the exact same issue and I resolved it by putting the below plugin in my POM file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</configuration>
</plugin>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
As ali4j mentioned in comment, using env variable is appropriate solution for this scenario. Use a custom env variable in POM and ask the users to set the env in their system.
How to refer environment variable in POM.xml?
Related
I have created a spring boot project (Project A) and I have run the build as maven install. This has created an executable jar file in the .m2 folder.
I want to use the above project as a dependency in two other spring boot projects(Project B and Project C). I have added the dependency of the project A in pom.xml of project B. But the dependency is being included as a folder under Maven Dependencies. I am only able to import required classes from project A, if project A is open in local project explorer. If I close Project A, the dependencies are not being resolved and it is throwing an error.
Am I doing something wrong while building the first project (A). Below is the build tag in pom.xml for project A. Can we add an executable file as a dependency?
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>**</include>
</includes>
</resource>
</resources>
<finalName>${project.artifactId}-${project.version}${build.number}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Please help me understand if there is something wrong here. Thanks in Advance :)
By default each Spring Boot application is inherited from Spring Boot's artifact so loads lot of dependent libraries automatically via maven's 'dependencyManagement' feature. Also due to same reason - not all required libraries will be automatically resolved as dependencies when Spring Boot application is turned into shared library.
To solve this, you need extract shared classes into dedicated shared library and use this library as dependency between projects.
No simple way, sorry.
I have a log4j.xml file in src/main/resources that I'm trying to exclude from the jar file. I have a property with version 3.0.0 in the parent pom file.
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/log4j.xml</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<configuration>
<!-- specify UTF-8, ISO-8859-1 or any other file encoding -->
<encoding>UTF-8</encoding>
</configuration>
</plugin>
...
According to maven manual (https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html), you have to put the files to include/exclude in resources section. So, if the file is located on src/resources, the configuration will be:
<project>
...
<name>My Resources Plugin Practice Project</name>
...
<build>
...
<resources>
<resource>
<directory>src/resources</directory>
<excludes>
<exclude>**/log4j.xml</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>
Hope that helps.
I upgraded to version 3.0.0 of the maven-jar-plugin and also changed the exclude back to "**/log4j.xml" from "src/main/resources/log4j.xml", which I had temporarily switched it to, so I'm not sure if one ore both fixes were required.
If you use <exclude>**/*log4j*</exclude> It should work if this pattern is acceptable for you.
Note what's written at the bottom of the doc: https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html
Note that the patterns need to be relative to the path specified for the plugin's classesDirectory parameter.
I have a package de.ht.ak.praktikum.hook in a project which is in a source folder src but it should be excluded from build path. I used to do this with right click on it and choosing Build Path -> Exclude. Since I added maven to the project every time I update the project the excluded folder turns into package again, i.e. the exclusion gets deleted. I tried to fix it this way:
...
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>de/ht/ak/praktikum/hook</exclude>
</excludes>
</resource>
</resources>
...
</build>
...
I tried also to do it as described there:
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>3.1</version>
<configuration>
<sourceExcludes>
<sourceExclude>de/ht/ak/praktikum/hook/</sourceExclude>
</sourceExcludes>
<sourceIncludes>
<sourceInclude>**/*.java</sourceInclude>
</sourceIncludes>
</configuration>
</plugin>
...
However none of the both methods help. Any ideas?
Your first attempt won't work because you're specifying to exclude it as a resource (i.e., those files that get packaged in your resulting JAR file - you don't usually want source files to be among them).
The second attempt is more on the right track. However, you want to exclude them from compilation, hence you need to set the exclude option of the maven-compiler-plugin. I.e., like this:
<build>
..
<plugins>
..
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<excludes>
<exclude>de/ht/ak/praktikum/hook/*.java</exclude>
</excludes>
</configuration>
</plugin>
..
</plugins>
</build>
When updating the project in Eclipse (Maven -> Update Project), Eclipse should honor this configuration and also exclude it from the Eclipse-internal build path.
Currently I have a sample maven project with the following build part where I specify a second resource location directory:
<project>
...
<build>
<resources>
<resource>
<directory>src/main/second-resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>${artifactId}</finalName>
<outputDirectory>${jarBuildPath}</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
I can get my jar packaged with this configuration having my classes and resource files just in right place and with no issue.
But when adding the property <classesDirectory> to my maven-jar-plugin configuration snippet with value src/main/java:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<classesDirectory>src/main/java</classesDirectory>
<finalName>${artifactId}</finalName>
<outputDirectory>${jarBuildPath}</outputDirectory>
</configuration>
</plugin>
then when packaging my archive, hell, my resource files are no longer there.
From maven official documentation for the classesDirectory property, it says that Directory containing the classes and resource files that should be packaged into the JAR. and that makes all sense for me and it is quite fair that my resource files get disappeared since maven assumes that no file has the right to be packaged unless it is under src/main/java.
But my big thought was when specifying my resource files location (with one of the options below), Maven will be aware of the files location even if I had specified the <classesDirectory> entry.
With top level <resources> entry
<resources>
<resource>
<directory>src/main/second-resources</directory>
</resource>
</resources>
or via the maven-resource-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<resources>
<resource>
<directory>src/main/second-resources</directory>
</resource>
</resources>
</configuration>
</plugin>
I can tell you that I'm quite familiar with maven but this issue seemed weird for me: Is it not recommended to have in maven-jar-plugin when you have a some custom resource directory?
Can anyone kindly drop some light on this?
BR.
The classesDirectory is the directory where
the compiler plugin stores compiled java classes
the resources-plugin copies resources to be included in the file
created by the jar-plugin.
You have changed it to your java source code directory in the jar-plugin configuration.
Therefore, I imagine that your jar now only contains java source files.
Is this what you want, or are you just trying to include a second source of resource files?
If the latter, then just adding:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/second-resources</directory>
</resource>
</resources>
should accomplish what you want without adding any configuration to the jar plugin. Note that the first <resource>..</resource> section is needed if you still have stuff you want in the default src/main/resources location.
By default, files under web-app are served, in an application container like tomcat, as static resources. So if I save a file like helloworld.html in those I can view it from my browser at
http://localhost:8080/myapp/helloworld.html
Is it possible to configure additional folders other than web-app to serve static content. I would prefer to do this from within the war file itself instead of having to configure it on a tomcat-wide way.
Any suggestions?
Maven's war plugin is a good fit here. This offers several configuration options when building the war so it'll be independent of your web server.
And an example where a different directory can be hosted as you suggest
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>resource2</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
Here's an another example they give where a different webapp folder can be used.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
</configuration>
</plugin>
</plugins>
</build>
...
</project>