Gradle - JARs copied into WEB-INF getting overwritten by GoogleAppEngine? - java

I'm currently using Gradle to compile and upload my Java code to GoogleAppEngine. It compiles fine locally, the problem is that the JAR libraries that the code is dependent upon are not within the "WEB-INF" folder on GAE.
I'm currently using War to copy the JAR files to WEB-INF using:
copy {
from 'libs'
into 'build/exploded-war/WEB-INF'
include '**/*.jar'
}
The problem is that if it does this before the "gaeUpdate" command then the WEB-INF folder is overwritten and the JARs are deleted and thus not uploaded. If it does it after the gaeUpdate command then it's too late, the files have already been uploaded to the server and they are thus not uploaded.
It's important to note that if I enter "gradle" in the console without the "gaeUpdate" then the JARs don't get deleted, it's only when the "gaeUpdate" is added that they appear to get removed.
Is there way to upload the files after the "build" folder has been created but before the server upload?

Related

Creating war from its component folders

I have deployed my war file on a remote linux server. I run this war using jetty-runner. Its not feasible for me to push this war multiple time. Its size is huge and it takes aprrox 45 min to push a fresh war onto the server. To handle this issue I thought of using the following steps(with commands) :
unzip:Unzip war to its corresponding files/folders : WEB-INF, META-INF, index.jsp.
Updating new class file in WEB-INF.
zip:Repacking these folder into a war again.
But the newly created war does not work. Is there a standard/correct way to pack these files into a war. Also, jar command is not available on the server.
Please suggest.
P.S. Already looked into various SO questions but didn't find any useful solution.
The zip command does not work as expected. The war packed by that command did not work. Instead, we have to use the JAR command.
I was able to generate the war after modifying the contents by using :
jar -cvf webproject.war index.jsp META-INF/ WEB-INF/
Note: If the jar command is not available on the server, specify JAR path using installed java on the server:
PATH_TO_JAVA/bin/jar -cvf webproject.war index.jsp META-INF/ WEB-INF/

Can't Access resource path, only the target path

I want to access the resource's form of my project "\src\main\resources" but for any reason I can only access the target classes.
Here is my code:
System.out.println(Main.class.getResourceAsStream("/123.txt")); // java.io.BufferedInputStream#66cd51c3
System.out.println(Main.class.getResource("/123.txt")); // file:/C:/Users/Raul/workspace/Serial/target/classes/123.txt
System.out.println(Thread.currentThread().getContextClassLoader().getResource("123.txt").getPath()); // /C:/Users/Raul/workspace/Serial/target/classes/123.txt
and here my Project Dirs:
The thing is, even if I delete all the files in the target/classes and run the code, the compiler will copy the files from "src/main/ressources" into "target/classes" and read them from there.
I want to access the resource's form of my project "\src\main\resources" but for any reason i can only access the target classes.
I think the question is answered by user #VGR. Just to clarify it in another words:
You put your resources in the /src/main/resources folder, and these resouces will be copied as is into the /target/classes folder when you build your project.
Example
src/main/resouces/123.txt -> target/classes/123.txt
src/main/resources/myresources/145.txt -> target/classes/myresources/145.txt
...
Now if you run the program inside of your IDE you'll observe the following:
System.out.println(Main.class.getResource("/123.txt"));
output: file:/C:/Users/Raul/workspace/Serial/target/classes/123.txt
System.out.println(Main.class.getResource("/myresources/145.txt"));
output: file:/C:/Users/Raul/workspace/Serial/target/classes/myresources/145.txt
But if you open the generated jar file you'll not see the target folder because the file 123.txt will be on the root of the jar file and the file 145.txt will be under the folder myresources/145.txt.
The folder target is just an output directory for the build tool and will not be packaged within your jar file.
Now to the following question:
the problem is that i dont know how to export the target classes to my jar, or how can I get "src/main/ressources" as return value.
To answer this question you have to look into your pom.xml file on the root of your project. There should be a <packaging>jar</packaging> entry in it. If that is so you create the jar file as follows:
Option 1: from the command line
mvn clean install
the jar file will be created and copied into the folder target.
Option 2: from within Eclipse (for example)
right click on the pom.xml > Run AS > Maven install
the jar file should also be generated and copied into the folder target.
Note: on your screenshot there are two jar files: core-0.0.1-SNAPSHOT.jar and Serial-0.0.1-SNAPSHOT.jar; remove them (mvn clean or right click > Run AS > Maven clean) before generating the jar file. The reason is Maven can only generate one jar file per Maven module / project, afaik.
You are seeing the intended behavior. A Java program is compiled into an executable form—meaning, .class files and resources. When other users run your program, they will not have access to the source, so your code should not assume your source tree will be available.
Simply put, your code is correct as is. Do not attempt to read the source tree. If you want target/classes to contain up-to-date files, rebuild your project.
A word of caution: Never use the getPath() method of URL to convert a URL to a file name. There are many characters which are not permitted in URLs, and those characters will be “percent-escaped” in order to conform to the URL specification; as a result, the path portion of a URL is not a valid filename! The only correct way to convert a URL to a file is Paths.get(url.toURI()). However, you should not even try to convert a resource to a file at all, because once you move on to packaging your programs in .jar files, your resources will not exist as regular files at all, only as entries in .jar files (which are actually just zip files with some Java-specific entries added).

How to properly export a jar for running an applet?

I have been adding modifications to a Java application that runs on a server using an applet. On the server, there is just the jar file, and an index.html page that opens the applet.
Now, I am trying to test the new version of the app on my computer before putting it on the production server, and it doesn't work : the application doesn't connect with the MySQL DB (when I just run it from Eclipse it works, it is when I try to run the jar file from my browser that it doesn't work). I tried running the old app on my desktop the same way and it runs just fine, so I guess the problem is in my jar file.
I have opened the 2 jar archives to see what's inside, and here's what I get :
Old jar :
Different folders for the different packages of the app
bin folder
com folder
META-INF folder
org folder
.classpath file
.project file
java.policy.applet file
mysql-connector-java-5.0.8-bin.jar
And now, here is what's inside the new jar :
Different folders for the different packages of the app
META-INF folder
.classpath file
.fatjar file
.project file
java.policy.applet file
mysql-connector-java-5.0.8-bin.jar
settings.fatjar file
So my archive doesn't have the bin, com and org folders. It also has 2 additionnal files.
I don't know which settings should I use when exporting my jar in Eclipse to obtain the same content ?
Also, my new jar isn't signed, could this have something to do with my problem of database access ?
I'll answer my own question, as I found the solution :
The jar needs to be exported as a runnable jar
The option "extract required libraries into jar" must be selected
The jar must be signed (self signed works as it is an internal app, so no need to pay a certificate)
Now it works :)

Eclipse Remote Application Platform export resource folder into WAR

I am currently writing a RAP application and would like to export a folder named "repository" containing several files along with the WAR file created by the WAR Product Configuration file. However, the resource folder is never exported into the WAR. I tried setting several BuildPaths in the the Eclipse project, the Manifest.MF and plugin.xml file. None of that worked.
Also, I also still don't know how to get a path to a contained file/folder within the resource folder on disc. I am for example trying to load a Axis2 Repository from that resource folder and my code for that is the following:
context = ConfigurationContextFactory.createConfigurationContextFromFileSystem("/repository/", "/repository/conf/axis2.xml");
yet this always throws the error that the repository could not be found in the filesystem.
Just use WAR Product Tooling plugin to create the .war file, and make sure the repository folder is put into the same project.
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.rap.help%2Fhelp%2Fhtml%2Fadvanced%2Fdeployment.html

Netbeans:Java prevent Folder delete while cleaning project

I am creating a JSF application using Netbeans and i had a download folder called Snap and Files which stores the client uploaded Snaps and Files respectively but when i clean the project whole build folder gets deleted and so my files. Is there any way to prevent it from deleting some folder or any alternative.(I dont want to save it on any static path.)
You can and should not store the uploaded files in expanded WAR folder. They would in a real production environment also get lost simply because files which are added during runtime are not contained in the original WAR.
You should store them on a fixed path outside the deploy folder. There are several ways to configure the webapp to use it and to configure the server to serve files from the folder by a virtual path. See also this answer for a detailed explanation and examples: Uploaded image only available after refreshing the page

Categories