When running my project directly with java, I can confirm that a particular method is able to fetch a resource via getClass().getClassLoader().getResource(path), where path is docs/info.md and there exists a file at src/main/resources/docs/info.md
After the project is packaged with Maven into the final executable .jar, I can confirm that the path docs/info.md has been included via jar tf myproject.jar
However, when that .jar is executed, the same getClass().getClassLoader().getResource(path) returns null.
Here's myproject.pom:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<!-- io.outright.myproject [parent] -->
<parent>
<groupId>io.outright</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!-- io.outright.myproject -->
<groupId>io.outright.myproject</groupId>
<artifactId>hub</artifactId>
<version>1.0-SNAPSHOT</version>
<name>hub</name>
<!-- props -->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- deps -->
<dependencies>
<!-- ....removed for readability.... -->
</dependencies>
<!-- build -->
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<!-- Integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19</version>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!--<classpathPrefix>lib/</classpathPrefix>-->
<mainClass>io.outright.myproject.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>junit:junit</exclude>
<!--<exclude>log4j:log4j:jar:</exclude>-->
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Use getResourceAsStream instead of getResource. getResource returns the URL of file, not the file itself. Then write the InputStream to a file, and use that file.
ClassLoader classLoader = getClass().getClassLoader();
if (classLoader.getResourceAsStream(filePath) == null) {
// throw error
}
InputStream inputStream = classLoader.getResourceAsStream(filePath);
// write input stream to a file
I faced the same issue. This might help: Get directory path of maven plugin in its own Mojo
Related
I used 'mvn install' and 'mvn exec:java' to start my project, but the error occurred as the title. What exact 'Mainclass' should I set to 'mojo:exec-maven-plugin'? I tried to delete full m2.repository but it didn't work for me. I also tried to add some code to set the plugin but I don't know what mainclass should I set. Could anyone give me some advise?
my pom file shows below:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<developers>
<developer>
<id>TaehoonK</id>
<name>Taehoon Kim</name>
<email>taehoon.kim#pusan.ac.kr</email>
<organization>Pusan National University</organization>
<roles>
<role>Module Maintainer</role>
<role>Java Developer</role>
</roles>
</developer>
</developers>
<organization>
<name>STEMLab in Pusan National University</name>
<url>http://stem.pnu.edu</url>
</organization>
<properties>
<jar.name>simmApplication</jar.name>
<lib.dir>lib</lib.dir>
</properties>
<groupId>edu.pnu.stem</groupId>
<artifactId>OpenGDS_Conversion</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<!-- ======================================================= -->
<!-- JAR packaging. -->
<!-- ======================================================= -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>${jar.name}</finalName>
<outputDirectory>${project.basedir}</outputDirectory>
<archive>
<manifest>
<mainClass>edu.pnu.stem.convert.MainForm</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>${lib.dir}/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.basedir}/${lib.dir}</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<inherited>false</inherited>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}</directory>
<includes>
<include>VERSION.txt</include>
</includes>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<encoding>utf-8</encoding>
<maxmem>512M</maxmem>
</configuration>
</plugin>
</plugins>
</build>
</project>
Within a larger Maven project, I have a Java file (AudioFileBuilder.java) that generates audio files from the Google Cloud Text-to-Speech service. It can execute from its own main() class; however, this file references an enumeration within the project (that other project source files depend on).
I am creating a fat JAR (JAR-with-dependencies), .exe file (Windows), and .app file (macOS) for the main project. I would like to exclude the AudioFileBuilder.java file AND the associated Google Cloud Text-to-Speech dependency in the JAR, exe, and app files. However, I still need the AudioFileBuilder.java file to run every time the project is built.
Here is a link to my project's current POM file (the project is open-source):
https://github.com/hadi16/GamesForTheBlind/blob/Alex-Branch/pom.xml
This is my current POM file:
<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>edu.up</groupId>
<artifactId>Games_For_Blind</artifactId>
<version>1.0.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<packaging>jar</packaging>
<dependencies>
<!-- Compile Dependencies -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-texttospeech</artifactId>
<version>0.117.0-beta</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
<scope>compile</scope>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>12</source>
<target>12</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>gamesforblind.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-classes</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target/xsd-out</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- To generate the phrase audio files. -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>gamesforblind.synthesizer.AudioFileBuilder</argument>
</arguments>
<environmentVariables>
<GOOGLE_APPLICATION_CREDENTIALS>google_api_key.json</GOOGLE_APPLICATION_CREDENTIALS>
</environmentVariables>
</configuration>
</plugin>
<!-- Create executable Windows file. -->
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.7.25</version>
<executions>
<execution>
<id>l4j-clui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>gui</headerType>
<jar>${project.build.directory}/dist/${project.artifactId}-${project.version}-jar-with-dependencies.jar</jar>
<outfile>${project.build.directory}/dist/${project.artifactId}-${project.version}.exe</outfile>
<classPath>
<mainClass>gamesforblind.Main</mainClass>
</classPath>
<jre>
<minVersion>12</minVersion>
<jdkPreference>preferJre</jdkPreference>
</jre>
<versionInfo>
<copyright>2019</copyright>
<fileVersion>${project.version}</fileVersion>
<txtFileVersion>${project.version}</txtFileVersion>
<fileDescription>${project.name}</fileDescription>
<productVersion>${project.version}</productVersion>
<txtProductVersion>${project.version}</txtProductVersion>
<productName>${project.name}</productName>
<internalName>${project.name}</internalName>
<originalFilename>${project.artifactId}-${project.version}.exe</originalFilename>
<companyName>University of Portland</companyName>
</versionInfo>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>de.perdian.maven.plugins</groupId>
<artifactId>macosappbundler-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<plist>
<CFBundleDisplayName>Games For The Blind</CFBundleDisplayName>
<JVMMainClassName>gamesforblind.Main</JVMMainClassName>
<JVMVersion>12</JVMVersion>
<CFBundleDisplayName>Games For The Blind</CFBundleDisplayName>
<CFBundleName>Games For The Blind</CFBundleName>
</plist>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
UPDATE:
I divided the project into two separate modules: "audio-builder" & "application" - as suggested by Welsh.
However, I have a Java enumeration ("Phrases.java") that both the audio-builder & application uses. The audio-builder needs this file to know which audio phrases are currently in the program, while the rest of the application needs this to load in said audio files & call the playback of these files.
So, this made me add the "audio-builder" module as a dependency for the "application" module. Since the Google Cloud dependency is needed at compile-time, this means that it is bundled in the generated JAR, app, and exe files.
These changes can be found here: https://github.com/hadi16/GamesForTheBlind/tree/Maven-Fixes
Root POM file:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.up</groupId>
<artifactId>games-for-the-blind</artifactId>
<version>1.0.0.0</version>
<modules>
<module>games-for-the-blind/audio-builder</module>
<module>games-for-the-blind/application</module>
</modules>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Audio-builder POM file:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.up</groupId>
<artifactId>audio-builder</artifactId>
<version>1.0.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-texttospeech</artifactId>
<version>0.117.0-beta</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>12</source>
<target>12</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>builder.AudioFileBuilder</argument>
</arguments>
<environmentVariables>
<GOOGLE_APPLICATION_CREDENTIALS>google_api_key.json</GOOGLE_APPLICATION_CREDENTIALS>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
Application POM file (note the dependency to audio-builder):
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.up</groupId>
<artifactId>application</artifactId>
<version>1.0.0.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Compile Dependencies -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
<scope>compile</scope>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>edu.up</groupId>
<artifactId>audio-builder</artifactId>
<version>1.0.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>12</source>
<target>12</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>gamesforblind.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-classes</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target/xsd-out</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Create executable Windows file. -->
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.7.25</version>
<executions>
<execution>
<id>l4j-clui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>gui</headerType>
<jar>
${project.build.directory}/dist/${project.artifactId}-${project.version}-jar-with-dependencies.jar
</jar>
<outfile>${project.build.directory}/dist/${project.artifactId}-${project.version}.exe
</outfile>
<classPath>
<mainClass>gamesforblind.Main</mainClass>
</classPath>
<jre>
<minVersion>12</minVersion>
<jdkPreference>preferJre</jdkPreference>
</jre>
<versionInfo>
<copyright>2019</copyright>
<fileVersion>${project.version}</fileVersion>
<txtFileVersion>${project.version}</txtFileVersion>
<fileDescription>${project.name}</fileDescription>
<productVersion>${project.version}</productVersion>
<txtProductVersion>${project.version}</txtProductVersion>
<productName>${project.name}</productName>
<internalName>${project.name}</internalName>
<originalFilename>${project.artifactId}-${project.version}.exe</originalFilename>
<companyName>University of Portland</companyName>
</versionInfo>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>de.perdian.maven.plugins</groupId>
<artifactId>macosappbundler-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<plist>
<CFBundleDisplayName>Games For The Blind</CFBundleDisplayName>
<JVMMainClassName>gamesforblind.Main</JVMMainClassName>
<JVMVersion>12</JVMVersion>
<CFBundleDisplayName>Games For The Blind</CFBundleDisplayName>
<CFBundleName>Games For The Blind</CFBundleName>
</plist>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Thank you!
I would recommend that you split your projects into two sub projects, so you would have for example:
games-for-the-blind/
pom.xml
audio-builder/
pom.xml
application/
pom.xml
Then what you can do is have the audio-builder package output the required files into the application/resources directory and have it listed first in the main pom.xml file.
This would allow you to distribute the application resultant files that would not contain any of the details of the audio-builder project.
I have a project like this:
TestArt
-----------index.jsp
-----------pom.xml
-----------src
--------------main
------------------java/com/web/LoginServlet.java
------------------webapp/WEB-INF/classes/com/web/LoginServlet.class
-----------target
-----------------testproject-1.0-SNAPSHOT.war
-----------------testproject-1.0-SNAPSHOT.war.original
-----------------testproject-1.0-SNAPSHOT
----------------------------------META-INF
----------------------------------WEB-INF
---------------------------------------classes/com/web/LoginServlet.class
---------------------------------------lib (here are all the jars)
and pom.xml like this:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.util</groupId>
<artifactId>testproject</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test Project</name>
<url>https://test.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
...
</dependencies>
<properties>
<java.version>1.7</java.version>
<start-class>com.web.LoginServlet</start-class>
</properties>
<build>
<outputDirectory>${basedir}\src\main\webapp\WEB-INF\classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.github.jsimone</groupId>
<artifactId>webapp-runner</artifactId>
<version>8.0.30.2</version>
<destFileName>webapp-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<defaultGoal>install</defaultGoal>
</build>
</project>
After I run mvn package it creates META-INF and WEB-INF folders inside root/target folder and also places .war files in there.
I want maven to also copy a WEB-INF folder from target into the root (TestArt) once the packaging is done. Is there any way to achieve it?
I really would recommend to spend time on fixing the original problem, but if you want:
Use Maven Antrun Plugin to copy a folder from one place to another, as described here:
https://stackoverflow.com/a/694175/927493
The details of the copy command in Ant are explained here:
https://ant.apache.org/manual/Tasks/copy.html
Couldn't figure it out with Ant, so I used this answer. In my case it would look like this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>target/testproject-1.0-SNAPSHOT/WEB-INF</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
EDIT: This plugin copy pastes files one by one, instead of taking a folder and copy pasting it with all it files once. This leads to broken links between classes and directories and nothing seems to work. For now, I'm using the batch script:
xcopy "C:\...\Tomcat 8.5\webapps\TestArt\target\testproject-1.0-SNAPSHOT\WEB-INF" "C:\...\Tomcat 8.5\webapps\TestArt\WEB-INF\" /E /Y
Using this answer's Maven plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>sign</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<exec executable="${basedir}\copy.bat">
</exec>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
I managed to achieve a desired result.
But if there are solutions in pure Maven that would copy a directory all at once, I'd love to hear them.
I want to wrap my jar with launch4j to an exe file. Here is my 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>de.thz.cameracontrol</groupId>
<artifactId>Kamerasteuerung</artifactId>
<version>0.0.3-SNAPSHOT</version>
<packaging>jar</packaging>
<name>name</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<version.maven.compiler>2.3.2</version.maven.compiler>
<version.maven.site>3.3</version.maven.site>
<version.sonar>4.3</version.sonar>
<artifact.id>Kamerasteuerung</artifact.id>
</properties>
<profiles>
<!-- profiles for repositories and distribution management -->
</profiles>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.maven.compiler}</version>
<configuration>
<encoding>UTF-8</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>${version.maven.site}</version>
</plugin>
<!-- LAUNCH4J -->
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<executions>
<execution>
<id>l4j-clui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>console</headerType>
<jar>target/Kamerasteuerung-0.0.3-SNAPSHOT-jar-with-dependencies.jar</jar>
<outfile>target/CameraControl.exe</outfile>
<downloadUrl>http://java.com/download</downloadUrl>
<classPath>
<mainClass>de.thz.cameracontrol.server.Server</mainClass>
</classPath>
<jre>
<bundledJre64Bit>false</bundledJre64Bit>
<bundledJreAsFallback>false</bundledJreAsFallback>
<minVersion>1.8.0</minVersion>
<jdkPreference>preferJre</jdkPreference>
<runtimeBits>32</runtimeBits>
</jre>
<versionInfo>
<fileVersion>1.0.0.0</fileVersion>
<txtFileVersion>${project.version}</txtFileVersion>
<fileDescription>${project.name}</fileDescription>
<copyright>C</copyright>
<productVersion>1.0.0.0</productVersion>
<txtProductVersion>1.0.0.0</txtProductVersion>
<productName>${project.name}</productName>
<internalName>AppName</internalName>
<originalFilename>CameraControl.exe</originalFilename>
</versionInfo>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>de.thz.cameracontrol.server.Server</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- LAUNCH4J ENDE -->
</plugins>
</pluginManagement>
</build>
<dependencies>
...
</dependencies>
And this is my batch:
#echo off
call mvn -D skipTests clean assembly:attached
pause
I get a correct jar with my dependencies but i dont get an exe file. Is there something else to do? Is an descriptor.xml really necessary?
Though I did not check your pom properly. As it is producing correct jar, I think it is fine.
Did you try mvn package ?
I'm new to Maven and I'm trying to configure Maven to generate 2 jars: one for development and one for production. The only difference between them is a config.properties file that have the database connection different so I thought I could use Maven profiles.
To my surprise I can't generate both files at once. When using profiles, each time you build you have to select the profile and a jar (in my case) will be created using the profile. The thing is that it will create 2 exactly equals jars, one without a classifier and one with the classifier (like myjar.jar and myjar-prod.jar) so if I want to generate the dev and the prod jar I have to create 4 jars (running first Maven with one profile and after that with another profile)
Why is this? Doesn't make any sense to me... but ok...
My question is:
Is there a way I could avoid the two jars from being generated? I mean, I want to have different profiles, and I have accepted (with grief) to execute multiple times the build process (one for each profile), could I avoid to have each time 2 jars and have only one without the classifier?
This is my 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.p2p.</groupId>
<artifactId>LoadACHFiles</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyProject</name>
<url>http://maven.apache.org</url>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>config-*.properties</exclude>
</excludes>
</resource>
</resources>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.jasypt</groupId>
<artifactId>jasypt</artifactId>
<version>1.9.0</version>
<type>jar</type>
</dependency>
</dependencies>
<profiles>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--<delete file="${project.build.outputDirectory}/config.properties"/>-->
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
If you are okay with having classified jars, you may do what you want without profiles, so you may have jars for all environments with a single build command. The key is to understand how Maven filtering works.
This is expanding on an answer I provided to a similar question. Start with that setup. Then:
Create config.properties in your src/main/resources, containing properties your app needs.
my.database.url=${database.url}
my.database.user=${database.user}
my.database.pw=${database.pw}
Now, create prod.properties and dev.properties in ${basedir}/src/main/filters holding appropriate values for each environment.
database.url=URL-for-dev
database.user=user-for-dev
database.pw=pw-for-dev
When you run mvn clean package, Maven will copy the contents of /src/main/resources, including config.properties, doing property replacement during the copy. Because there are multiple executions of both resources and jar plugins, Maven will create separate classified jar files. Each will contain a config.properties file, holding the correct properties for the environment. The filters will not end up in the built jars.
I made it removing the maven jar plugin in the profile section. Changed this:
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--<delete file="${project.build.outputDirectory}/config.properties"/>-->
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<source>1.6</source>
<target>1.6</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
For this:
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--<delete file="${project.build.outputDirectory}/config.properties"/>-->
<copy file="src/main/resources/config-prod.properties"
tofile="${project.build.outputDirectory}/config.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>