Compiling one line instead of another in Java

I have a program that links to a file on our network drive ("PricingMaster.omas"), but when I'm in beta, I use another file ("PricingMasterBetaTest.omas"). Currently, I manually change the line from one to another.

So:

File masterFile = new File("g:/DataTeam", "PricingMasterBetaTest.omas"); 

becomes:

 File masterFile = new File("g:/DataTeam", "PricingMaster.omas"); 

In fact, this happens in several places in a pair of files, so as you can imagine, sometimes I forget, and my users end up getting confused. So I was wondering if there is a way to do this so that I can determine which line is used depending on what I am compiling it for?

I tried asking Google, but I don’t have enough knowledge to correctly formulate the question. In my opinion, the psuedo code looks something like this:

 @CompileDebug File masterFile = new File("g:/DataTeam", "PricingMasterBetaTest.omas"); @CompileRelease File masterFile = new File("g:/DataTeam", "PricingMaster.omas"); 

I am doing this project in Eclipse, so maybe this is a trick that can help me with Eclipse? I also do not mind doing it in Ant, if possible. Sorry, I know that I'm a little vague (or tight), today I feel bad in my Google Fu.

+4
source share
6 answers

You mostly run into this problem because you are hard-coded on your lines. Ideally, you should select such lines in a .properties file, where you could point to another file without recompiling the program.

And it’s not so difficult. Eclipse comes with a built-in Externalise Strings function that processes all the Java code for you. Everything else will just crack the problem.

Eclipse Source Menu:

Externalise strings

+3
source

Personally, I would override the problem and pass the file name on the command line:

 java -Dfile=PrixingMaster.omas -jar foo.jar 

Or use something like β†’

 @Option(name='file') private String filename; 

Either an options file, or a flag, or something based on the current computer or user, etc.

+3
source

Actually, placing parameters, such as a file name in the code, is bad practice, even if it is done at compile time, as you think.

The best solution would be to actually parameterize your application. Is this a simple console application? Then you can simply add the file name as a console parameter and read it in the main method.

0
source

One option is to put the string in the Constants class so that you don't have duplicate rows everywhere.

 public class Constant { public static final String FILE_PATH= ""; } 

Another option is to put this line in the properties file and load it when the application starts. Since you have two builds (debugging and release), you probably have two build / run scripts. You should also have two properties files: testconfig.properies and config.properties , which contain different file paths. Each of them must be included in the executable jar (or, nevertheless, you are deploying the application), depending on whether it is debugged or debugged.

0
source

You can have a Config class, and you can have a static variable called networkDrive.

 public class Config{ //public static String networkDrive = "PricingMasterBetaTest.omas"; //DEBUG public static String networkDrive = "PricingMaster.omas"; //RELEASE } 

When you want to use "PricingMasterBetaTest.omas", just comment out the line DEBUG and highlight the line RELEASE.

 File masterFile = new File("g:/DataTeam", Config.networkDrive); 
0
source

Put the file name in the properties file and use the resource package for it.

Where do you keep your test classes? Do you have parallel hierarchies such as src/main/java and src/test/java common in Maven projects, or src and test , as you could have in Ant and Eclipse? If you do, then use the identical property file names in the appropriate places and make sure that the test hierarchy precedes the src hierarchy in the list of source folders or in the Ant classes. I think the file that appears first wins the class paths. You have to do something manually with Ant, play with options in Eclipse, or do everything pretty well with Maven.

ANT

So your hierarchies will be

  • CSI
    • com
      • jaycarr
        • Omas
          • pricing.properties
  • test
    • com
      • jaycarr
        • Omas
          • pricing.properties

And define the paths to the classes:

 <path id="lib.classpath"> ... </path> <!-- Additional libraries for testing. --> <path id="testlib.classpath"> ... </path> <path id="compile.classpath"> ... <pathelement location="src"/> <path refid="lib.classpath"/> </path> <path id="test.classpath"> <path refid="testlib.classpath"/> <pathelement location="test"/> <path refid="compile.classpath"/> </path> 

Test the class’s test path to win the /../priceicing.properties test.

Maven

Just use

  • CSI
    • Main
      • Java
        • com
          • jaycarr
            • Omas
              • pricing.properties
    • test
      • Java
        • com
          • jaycarr
            • Omas
              • pricing.properties
Everything is ready.

Eclipse

This is the hardest thing. I am not sure how to exclude test libraries and folders from the main assembly. This is not a complete answer.

You can put your tests in a new project, which depends on your core. This will prevent your tool from being accidentally deployed with junit.jar.

0
source

Source: https://habr.com/ru/post/1488172/


All Articles