How to include python files of Java project into .war with maven? - java

I have Java project made with maven. So I have typical maven project layout. And I use Jython. So I got few python files. Wich I use through PythonInterpreter in Java classes.
I place my python files in src/main/py folder. And I use this path to import the modules by interpreter. It works fine on my laptop.
The problem is:
When I do mvn install, this folder does not goes to the war.
I read about maven resources plugin and added this folder as a resource. Like this:
<resources>
<resource>
<directory>src/main/py</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
I that case it adds everything that folder contents, to web-inf/ directly, but not in the src/main/py. So that path is invalid for application in war archive.
Question is:
How should I place this python resource and what I should write in pom.xml, to be able to use the same path on the laptop, and the server?

Can you try including targetPath in the resource element as below:
<resource>
<targetPath>../</targetPath>
<directory>src/main/py</directory>
<includes>
<include>**/*.py</include>
</includes>
</resource>

I would suggest to use the src/main/scripts folder and check if they where packaged into the war if this does not work check the documentation of the maven-war-plugin and define it with the war plugin as described in the documentation.

<resources>
<resource>
<directory>src/main/py</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
is actually enough to include into jar/war file.
For running see
Compile Python Sources in Maven
It is not recommended to use src/main/py in your python script,
think as src/main/py as working/current folder for you python

Related

source [Beans.xml] cannot be opened because it does not exist

I'm trying to package a Spring Project with Maven, but the Beans.xml file is missing.
A lot of people suggest to put the Bean.xml file in the main/resources, but it does not work for me. I think that only works for Eclipse user and I'm just compiling in the terminal using 'mvn clean install'.
Stupid mistake, inside my pom file the directory was like the following:
<build>
<resources>
<resource>
<directory>src/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.jpeg</exclude>
<exclude>**/*.gif</exclude>
</excludes>
</resource>
My resources folder was not inside src/main/resources, but src/resources.
And when I open my jar file the Beans.xml file this there and the code runs without error.

Non-java resources disappear from class package after maven packaging

I'm trying to create a Spring project structure where I have my HTML templates stored in the same src/main/java package (in Tapestry-like way), so the structure basically looks like this:
src/main/java
-org
-example
-views
-pageOne
PageOneController.java
Template.html
-pageTwo
PageTwoController.java
Template.html
However after packaging a WAR while using Maven I get a structure like this:
WEB-INF/classes
-org
-example
-views
-pageOne
PageOneController.class
-pageTwo
PageTwoController.class
So the .html files are ignored and obviously nothing works.
I tried to configure maven-resources-plugin to copy those resources like this:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>false</filtering>
<includes>
<include>*/**.html</include>
</includes>
<excludes>
<exclude>*/**.java</exclude>
</excludes>
</resource>
</resources>
</build>
but to no avail.
How do I configure my project to copy non-java files from src/main/java into the final output?
It turns out that the problem was with maven modules. I have declared resources plugin in the main pom.xml instead of the module's pom.xml which lead to the configuration issues.

How do I embed artifact information in maven shaded jar

I would like to document in the shaded jar what maven artifacts actually end up in that shaded jar.
All the packages get merged and that makes it difficult to workout exactly what artifacts went into it just by looking at the jar.
I suppose the ideal place for that information would be the manifest file but it could just be in a text file.
Ideally I want to see groupId, artifactId and version.
Is this at all possible with the maven shade plugin?
Thanks in advance, Phil.
You can do this with maven, below steps to follow:
1- Create under src/main/resources a file wich will contain the information, information.txt for example with the following content:
version=${project.version}
artifactId=${project.artifactId}
groupId=${project.groupId}
2- Activate Maven filtring
<project>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/information.txt</include>
</includes>
</resource>
...
</resources>
...
</build>
...
</project>
3- Build your project. The file will contain now the information you need.
More information about the plugin.
As it doesn't like this is supported by the shade plugin i have requested this feature.
https://issues.apache.org/jira/browse/MSHADE-236
When building jars with Maven, by default you will get the following entries: /META-INF/maven/${groupId}/${artifactId}/pom.properties and /META-INF/maven/${groupId}/${artifactId}/pom.xml. When shading, all these files will also end up in the shaded jar.

How to retrieve <finalName> value from POM?

I manually set the tag value in my pom.xml before I packaged my Spring web app to .war and then I manually deploy my .war file on JBOSS.
I have an test.html file in webapp folder which is sending an POST request to test if #RequestMapping methods inside my controller class are working as expected or not. I want to store the value of tag in the JavaScript variable (in test.html).
How can I fetch the value from pom.xml and store in the JavaScript variable?
Make a filtered property resource file that has ${finalName} in it, and then read that in from your classpath.
src/main/resources/config.properties:
finalName=${propContainingFinalName}
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>config.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

.dat files not packaged in jar in Netbeans

I have a maven Java project projA that build into a jar.
I have another maven Web project projB that holds a dependency of projA.
In proj A one of my classes accesses the resources located in the packages in projA.
When I expand the jar shown under dependencies in projB, I see that it does not contain the resource files (.dat).
What setting I need to do to copy these files into the jar when I build my projA in Netbeans
I am using NetBeans IDE 8.0.2 / Java 1.8
In your pom.xml, include something along the line like this:
<build>
<resources>
<resource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes>
<include>**/*.dat</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
[...]

Categories